ACC2: How to Create a Stopwatch FormID: Q128813
|
Moderate: Requires basic macro, coding, and interoperability skills.
This article describes how to create and use a form that contains a
Start/Stop and a Reset command button that use the form's Timer event to
display elapsed hours, minutes, and seconds in a text box control.
This article assumes that you are familiar with Access Basic and with
creating Microsoft Access applications using the programming tools
provided with Microsoft Access. For more information about Access Basic,
please refer to the "Building Applications" manual.
The following example demonstrates how to create and use a form to track
elapsed time:
ScrollBars: Neither
RecordSelectors: No
NavigationButtons: No
OnTimer: [Event Procedure]
TimerInterval: 0
Sub Form_Timer ()
Dim Hours, Minutes, Seconds, MS
Dim Msg As String
ElapsedMS = ElapsedMS + Me.TimerInterval
Hours = Format((ElapsedMS \ 360000), "00")
Minutes = Format(((ElapsedMS \ 6000) Mod 60), "00")
Seconds = Format((ElapsedMS \ 100) Mod 60, "00")
MS = Format((ElapsedMS) Mod 100, "00")
If Hours > 0 Then Msg = Hours & ":"
Msg = Msg & Minutes & ":" & Seconds & ":" & MS
Me!ElapsedTime = Msg
End Sub
Name: StartStop
Caption: Start
OnClick: [Event Procedure]
Sub StartStop_Click ()
If Me.TimerInterval = 0 Then
Me.TimerInterval = 1
Me![StartStop].Caption = "Stop"
Else
Me.TimerInterval = 0
Me![StartStop].Caption = "Start"
End If
End Sub
Name: Reset
Caption: Reset
OnClick: [Event Procedure]
Sub Reset_Click ()
ElapsedMS = 0
Me!ElapsedTime = ""
End Sub
Name: ElapsedTime
Enabled: No
Locked: Yes
Option Compare Database
Option Explicit
Dim ElapsedMS
Keywords : kbusage FmsHowto
Version : 2.0
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: April 9, 1999