Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculate Time Elapsed Between Two Times

Access - Select Dialogs

Calculate Time Elapsed Between Two Times

by  SgtJarrow  Posted    (Edited  )
Access uses an internal Timer fucntion to calculate times....this function returns a Single number that is the number of seconds that have elapsed since midnight. The following problems can occur:
- turns over at midnight, in other words the number starts over
- it turns over after 24 hours, if your calculation is longer than 24 hours the results will be wrong
- not accurate, measures only 1/18-second accuracy

The below code and its usage is much more efficient. Reasons:
- it turns over every 49 days or so
- has no concept of days, so it does not turn over each day
- accurate to 1/1000-second and does not use the floating math of the Timer function

So if you really want to get exact...

Create a new class module called StopWatch
Put the following code in the StopWatch class module:

'*********** Start Code ****************
Private mlngStart As Long
Private Declare Function GetTickCount Lib "kernel32" () As Long

Public Sub StartTimer()
mlngStart = GetTickCount
End Sub

Public Function EndTimer() As Long
EndTimer = (GetTickCount - mlngStart)
End Function
' *********** End Code ****************

You use the code as follows:

' *********** Start Code **************
Dim sw as StopWatch
Set sw = New StopWatch
sw.StartTimer

' Do whatever you want to time here

Debug.Print "That took: " & sw.EndTimer & "milliseconds"
' ********* End Code *************

Remember that this returns milliseconds that have elapsed...so to get seconds, you must divide the result by 1000, minutes by 60000, etc.

Hope this helps you out.....
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top