ACC2: How to Create a Stopwatch Form

ID: Q128813


The information in this article applies to:
  • Microsoft Access 2.0


SUMMARY

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.


MORE INFORMATION

The following example demonstrates how to create and use a form to track elapsed time:

  1. Open any database.


  2. Create a blank form not based on any table or query and set the following properties for the form:
    
          ScrollBars: Neither
          RecordSelectors: No
          NavigationButtons: No
          OnTimer: [Event Procedure]
          TimerInterval: 0 


  3. Type the following code for the form's OnTimer property event procedure:
    
          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 


  4. Add a command button to the form and set the following properties for the command button:
    
          Name: StartStop
          Caption: Start
          OnClick: [Event Procedure] 


  5. Enter the following code for the command button's OnClick property 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 


  6. Add a second command button to the form and set the following properties for the second command button:
    
          Name: Reset
          Caption: Reset
          OnClick: [Event Procedure] 


  7. Enter the following code for the second command button's OnClick property event procedure:
    
           Sub Reset_Click ()
             ElapsedMS = 0
             Me!ElapsedTime = ""
           End Sub 


  8. Add a text box to the form and set the following properties for the text box:
    
          Name: ElapsedTime
          Enabled: No
          Locked: Yes 


  9. Click Code on the View menu to display the form module. In the Declarations section, replace the default statement
    
          Option Compare Database 

    with the statements:
    
          Option Explicit
          Dim ElapsedMS 


NOTE: The performance of the Stopwatch form will be dependent upon the computer's processor speed. The form may not display actual elapsed time and may vary form computer to computer.

For more information about creating a Stopwatch Form in Microsoft Access 7.0 and 97, please see the following article in the Microsoft Knowledge Base:

Q142871 ACC: How to Create a Stopwatch Form (95/97)

Keywords : kbusage FmsHowto
Version : 2.0
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: April 9, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.