Microsoft KB Archive/325238

= HOW TO: Create a Stopwatch Form in Access 2002 =

Article ID: 325238

Article Last Modified on 4/6/2006

-

APPLIES TO


 * Microsoft Access 2002 Standard Edition

-



This article was previously published under Q325238



Moderate: Requires basic macro, coding, and interoperability skills.

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).



For a Microsoft Access 2000 version of this article, see 233275.

IN THIS TASK
SUMMARY
 * Steps to Create a Stopwatch Form



SUMMARY
This article describes how to create and how to use a form that contains Start/Stop and Reset command buttons that use the Timer event of a form to display elapsed hours, minutes, and seconds in a text box control.

back to the top

Steps to Create a Stopwatch Form
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. The following example demonstrates how to create and how to use a form to track elapsed time:  Open any database.  Create a blank form not based on any table or any query and then set the following properties for the form:   ScrollBars: Neither RecordSelectors: No  NavigationButtons: No   OnTimer: [Event Procedure] TimerInterval: 0   Add a text box to the form and then set the following properties for the text box:   Name: ElapsedTime DefaultValue: &quot;00:00:00:00&quot; Enabled: No  Locked: Yes   Add a command button to the form and then set the following properties for the command button:   Name: btnStartStop Caption: Start OnClick: [Event Procedure]   Add a second command button to the form and then set the following properties for the second command button: <pre class="fixed_text">  Name: btnReset Caption: Reset OnClick: [Event Procedure] </li>  Click Code on the View menu to open the editor. Type the following lines in the Declarations section: Option Explicit Dim TotalElapsedMilliSec As Long Dim StartTickCount As Long Private Declare Function GetTickCount Lib &quot;kernel32&quot; As Long </li>  Set the OnTimer property of the form to the following event procedure: Private Sub Form_Timer

Dim Hours As String Dim Minutes As String Dim Seconds As String Dim MilliSec As String Dim Msg As String Dim ElapsedMilliSec As Long

ElapsedMilliSec = (GetTickCount - StartTickCount) + _ TotalElapsedMilliSec

Hours = Format((ElapsedMilliSec \ 3600000), &quot;00&quot;) Minutes = Format((ElapsedMilliSec \ 60000) Mod 60, &quot;00&quot;) Seconds = Format((ElapsedMilliSec \ 1000) Mod 60, &quot;00&quot;) MilliSec = Format((ElapsedMilliSec Mod 1000) \ 10, &quot;00&quot;)

Me!ElapsedTime = Hours & &quot;:&quot; & Minutes & &quot;:&quot; & Seconds & &quot;:&quot; _ & MilliSec

End Sub </li>  Set the OnClick property of the btnStartStop command button to the following event procedure: Private Sub btnStartStop_Click

If Me.TimerInterval = 0 Then StartTickCount = GetTickCount Me.TimerInterval = 15 Me!btnStartStop.Caption = &quot;Stop&quot; Me!btnReset.Enabled = False Else TotalElapsedMilliSec = TotalElapsedMilliSec + _ (GetTickCount - StartTickCount) Me.TimerInterval = 0 Me!btnStartStop.Caption = &quot;Start&quot; Me!btnReset.Enabled = True End If

End Sub

</li>  Set the OnClick property of the btnReset command button to the following event procedure: Private Sub btnReset_Click TotalElapsedMilliSec = 0 Me!ElapsedTime = &quot;00:00:00:00&quot; End Sub </li> Open the form in Form view to test the stopwatch.</li></ol>

back to the top

Additional query words: acc2002 timer

Keywords: kbhowtomaster KB325238

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.