Time Difference

  • Thread starter Thread starter Bryan Hughes
  • Start date Start date
B

Bryan Hughes

Hello,

I am trying to figure out the difference between two time values.

The values are entered in medium time format.

8:00 AM and 10:15 AM but every time the value returned is 2.

Here is how I have it set up.

dtmTimeIn = Format(#8:00 AM#, "Medium Time")
dtmTimeOut = Format(#10:15 AM#, "Medium Time")

If IsDate(dtmTimeIn) And IsDate(dtmTimeOut) Then
intTime = DateDiff("n", dtmTimeIn, dtmTimeOut) / 60
Else
Exit Sub
End If

Me.txtD1Hours.Value = intTime

Why is this only returning 2? What am I missing?

-TFTH
Bryan
 
Assuming you declared intTime as an integer, all it can store is an integral
value.

DateDiff("n", dtmTimeIn, dtmTimeOut) is going to return 135 minutes, and
135/60 is 2.25, but putting that in an integer means you lose the .25

BTW, you've got far more code than you need. The Format function converts
the times to strings. Assuming you've declared dtmTimeIn and dtmTimeOut to
be Date variables, you're then forcing Access to coerce the strings to dates
to store them in the variables. And since dtmTimeIn and dtmTimeOut are date
variables, their contents MUST be dates, so that the IsDate checks are
unnecessary.

All you need is:

Dim dtmTimeIn As Date
Dim dtmTimeOut As Date
Dim sngTime As Single

dtmTimeIn = #8:00 AM#
dtmTimeOut = #10:15 AM#

sngTime = DateDiff("n", dtmTimeIn, dtmTimeOut) / 60
Me.txtD1Hours.Value = sngTime
 
Back
Top