DateDiff function

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm working on a time log and have a form with three text boxes, and I want
to subtract my finish time from my start time and put this in a third box.
The following code give me a 13 Type mismatch error.

Private Sub cmdStart_Click()
Me.txtStart = Format(Now, "mmmm dd yy, h:mm:ss AMPM")
End Sub

Private Sub cmdFinish_Click()
Me.txtFinish = Format(Now, "mmmm dd yy, h:mm:ss AMPM")
Me.txtTime = DateDiff("h", [txtStart] - [txtFinish])
End Sub

What am I doing wrong? Thanks!

John
 
Check the syntax for DateDiff in the help file. You want DateDiff to do the
subtracting for you, yet you are also using a subtraction equation in the
equation you've written. Instead, the two dates/times should be separate
parameters.
 
Thanks Wayne. I changed it to:

Me.txtTime = DateDiff("d", [txtStart], [txtFinish]) and I'm still getting
the type mismatch. All 3 text boxes are formatted as general date.

Any more thoughts?????

Wayne Morgan said:
Check the syntax for DateDiff in the help file. You want DateDiff to do the
subtracting for you, yet you are also using a subtraction equation in the
equation you've written. Instead, the two dates/times should be separate
parameters.

--
Wayne Morgan
MS Access MVP


Johnny Bright said:
I'm working on a time log and have a form with three text boxes, and I
want
to subtract my finish time from my start time and put this in a third box.
The following code give me a 13 Type mismatch error.

Private Sub cmdStart_Click()
Me.txtStart = Format(Now, "mmmm dd yy, h:mm:ss AMPM")
End Sub

Private Sub cmdFinish_Click()
Me.txtFinish = Format(Now, "mmmm dd yy, h:mm:ss AMPM")
Me.txtTime = DateDiff("h", [txtStart] - [txtFinish])
End Sub

What am I doing wrong? Thanks!

John
 
Both [txtStart], [txtFinish] are getting their values from the recordset not
the textboxes. You want to give your datediff the formated values from the
textboxes who's control source = [txtStart] and [txtFinish]
try this code out:

Private Sub Command2_Click()
'''''''''''''you can leave out these lines
Me.Text3 = "01/12/2005"
Me.Text5 = "01/14/2005"
''''''''''''
Me.Text0 = DateDiff("d", Me.Text3, Me.Text5) 'give datedif the textbox values

End Sub

Johnny Bright said:
Thanks Wayne. I changed it to:

Me.txtTime = DateDiff("d", [txtStart], [txtFinish]) and I'm still getting
the type mismatch. All 3 text boxes are formatted as general date.

Any more thoughts?????

Wayne Morgan said:
Check the syntax for DateDiff in the help file. You want DateDiff to do the
subtracting for you, yet you are also using a subtraction equation in the
equation you've written. Instead, the two dates/times should be separate
parameters.

--
Wayne Morgan
MS Access MVP


Johnny Bright said:
I'm working on a time log and have a form with three text boxes, and I
want
to subtract my finish time from my start time and put this in a third box.
The following code give me a 13 Type mismatch error.

Private Sub cmdStart_Click()
Me.txtStart = Format(Now, "mmmm dd yy, h:mm:ss AMPM")
End Sub

Private Sub cmdFinish_Click()
Me.txtFinish = Format(Now, "mmmm dd yy, h:mm:ss AMPM")
Me.txtTime = DateDiff("h", [txtStart] - [txtFinish])
End Sub

What am I doing wrong? Thanks!

John
 
Oops sorry, i forgot [txtStart], [txtFinish] does get it from the textboxes.
so i am not sure why its not working. i tested both ways and they work fine
so sorry i couldn't help out more.

visdev1 said:
Both [txtStart], [txtFinish] are getting their values from the recordset not
the textboxes. You want to give your datediff the formated values from the
textboxes who's control source = [txtStart] and [txtFinish]
try this code out:

Private Sub Command2_Click()
'''''''''''''you can leave out these lines
Me.Text3 = "01/12/2005"
Me.Text5 = "01/14/2005"
''''''''''''
Me.Text0 = DateDiff("d", Me.Text3, Me.Text5) 'give datedif the textbox values

End Sub

Johnny Bright said:
Thanks Wayne. I changed it to:

Me.txtTime = DateDiff("d", [txtStart], [txtFinish]) and I'm still getting
the type mismatch. All 3 text boxes are formatted as general date.

Any more thoughts?????

Wayne Morgan said:
Check the syntax for DateDiff in the help file. You want DateDiff to do the
subtracting for you, yet you are also using a subtraction equation in the
equation you've written. Instead, the two dates/times should be separate
parameters.

--
Wayne Morgan
MS Access MVP


I'm working on a time log and have a form with three text boxes, and I
want
to subtract my finish time from my start time and put this in a third box.
The following code give me a 13 Type mismatch error.

Private Sub cmdStart_Click()
Me.txtStart = Format(Now, "mmmm dd yy, h:mm:ss AMPM")
End Sub

Private Sub cmdFinish_Click()
Me.txtFinish = Format(Now, "mmmm dd yy, h:mm:ss AMPM")
Me.txtTime = DateDiff("h", [txtStart] - [txtFinish])
End Sub

What am I doing wrong? Thanks!

John
 
In that case, try

Me.txtTime = DateDiff("d", CDate([txtStart]), CDate([txtFinish]))

The values may be getting treated as text. Formatting changes the way they
look, not what they actually are. I assume that these are unbound textboxes,
so they don't have a field behind them to give a data type.
 
Back
Top