Timer Function

Description

Returns a Single representing the number of seconds elapsed since midnight.

Syntax

Timer

See Also

Randomize statement, Time function.

Example

This example uses the Timer function to pause the application. The example also uses DoEvents to yield to other processes during the pause.

Dim PauseTime, Start, Finish, TotalTime
If (MsgBox("Press Yes to pause for 5 seconds", 4)) = vbYes Then
    PauseTime = 5                            ' Set duration.
    Start = Timer                            ' Set start time.
    Do While Timer < Start + PauseTime
        DoEvents                                ' Yield to other processes.
    Loop
    Finish = Timer                            ' Set end time.
    TotalTime = Finish - Start            ' Calculate total time.
    MsgBox "Paused for " & TotalTime & " seconds"
Else
    End
End If
Example (Microsoft Access)

You can use the Timer function to time operations and events in your Microsoft Access application by placing the Timer function immediately before and after the operation in your code that you wish to time. The following example uses the name of a query as its argument, then calculates how long it takes the query to run.

Sub QueryTimer (strQueryName As String)
    Dim sngStart As Single, sngEnd As Single
    Dim sngElapsed As Single

    sngStart = Timer                            ' Get start time.
    DoCmd.OpenQuery strQueryName, acNormal        ' Run query.
    sngEnd = Timer                                ' Get end time.
    sngElapsed = Format(sngEnd - sngStart, "Fixed")    ' Elapsed time.
    MsgBox ("The query " & strQueryName & " took " & sngElapsed _
        & " seconds to run.")
End Sub