Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Microsoft: Access Modules (VBA Coding) FAQ

Access - Select Dialogs

Calculate Time Elapsed Between Two Times by SgtJarrow
Posted: 7 Aug 02 (Edited 10 Oct 02)

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

' 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.....

Back to Microsoft: Access Modules (VBA Coding) FAQ Index
Back to Microsoft: Access Modules (VBA Coding) Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close