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

ID: Q142871

The information in this article applies to:

SUMMARY

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

This article describes how to create and use a form that contains Start/Stop and Reset command buttons 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 Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.

NOTE: This article explains a technique demonstrated in the sample files, FrmSampl.exe (for Microsoft Access for Windows 95 version 7.0) and FrmSmp97.exe (for Microsoft Access 97). For information about how to obtain these sample files, please see the following articles in the Microsoft Knowledge Base:

   ARTICLE-ID: Q150895
   TITLE     : ACC95: Microsoft Access Sample Forms Available on MSL

   ARTICLE-ID: Q175066
   TITLE     : ACC97: Microsoft Access 97 Sample Forms Available on MSL

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. Add a text box to the form and set the following properties for the
    text box:

       Name: ElapsedTime
       DefaultValue: "00:00:00:00"
       Enabled: No
       Locked: Yes

 4. Add a command button to the form and set the following properties for
    the command button:

       Name: btnStartStop
       Caption: Start
       OnClick: [Event Procedure]

 5. Add a second command button to the form and set the following
    properties for the second command button:

       Name: btnReset
       Caption: Reset
       OnClick: [Event Procedure]

 6. Click Code on the View menu to display the form module. In the
    Declarations section, type the following:

       Dim TotalElapsedMilliSec As Long
       Dim StartTickCount As Long
       Private Declare Function GetTickCount Lib "kernel32" () As Long

 7. Type the following code for the form's OnTimer property 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), "00")
          Minutes = Format((ElapsedMilliSec \ 60000) Mod 60, "00")
          Seconds = Format((ElapsedMilliSec \ 1000) Mod 60, "00")
          MilliSec = Format((ElapsedMilliSec Mod 1000) \ 10, "00")

          Me!ElapsedTime = Hours & ":" & Minutes & ":" & Seconds & ":" _
             & MilliSec
       End Sub

 8. Type the following code for the btnStartStop command button's OnClick
    property event procedure:

       Private Sub btnStartStop_Click()
          If Me.TimerInterval = 0 Then
             StartTickCount = GetTickCount()
             Me.TimerInterval = 15
             Me!btnStartStop.Caption = "Stop"
             Me!btnReset.Enabled = False
          Else
             TotalElapsedMilliSec = TotalElapsedMilliSec + _
                (GetTickCount() - StartTickCount)
             Me.TimerInterval = 0
             Me!btnStartStop.Caption = "Start"
             Me!btnReset.Enabled = True
          End If
     End Sub

 9. Type the following code for the btnReset command button's OnClick
    property event procedure:

       Private Sub btnReset_Click()
          TotalElapsedMilliSec = 0
          Me!ElapsedTime = "00:00:00:00"
       End Sub

10. Open the form in Form view to test the stop watch.

For more information about creating a Stopwatch Form in Microsoft Access 2.0, please see the following articles in the Microsoft Knowledge Base:

   ARTICLE-ID: Q128813
   TITLE     : ACC2: How to Create a Stopwatch Form
Keywords          : kbusage FmsHowto 
Version           : 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto

Last Reviewed: November 21, 1998