Time Difference in Access using VBA

bacon333

Senior member
Mar 12, 2003
524
0
0
Here's the code:

Dim starttime As Date
starttime = Format(Now(), "nn:ss")
MsgBox starttime

[processes]

Dim endtime As Date
endtime = Format(Now(), "nn:ss")
MsgBox endtime

Dim finaltime As Long
finaltime = DateDiff("s", starttime, endtime)
MsgBox finaltime

I keep getting a "type mismatchtype: 0" error. Any ideas?
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
The Format function is re-casting starttime and endtime as String objects. DateDiff requires Date objects. Try:
 

Wizkid

Platinum Member
Oct 11, 1999
2,728
0
0
Format returns a string, try this instead:

Dim starttime As Date
starttime = Now()
MsgBox Format(starttime, "nn:ss")

'[processes]

Dim endtime As Date
endtime = Now()
MsgBox Format(endtime, "nn:ss")

Dim finaltime As Long
finaltime = DateDiff("s", starttime, endtime)
MsgBox finaltime
 

KLin

Lifer
Feb 29, 2000
30,131
506
126
change the formatting to hh:nn:ss. that should fix the problem.