ACC2000: How to Create a Stopwatch Form

ID: Q233275


The information in this article applies to:

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

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


SUMMARY

This article describes how to create and 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. Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals 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 needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp


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:


  3. 
       ScrollBars: Neither
       RecordSelectors: No
       NavigationButtons: No
       OnTimer: [Event Procedure]
       TimerInterval: 0 
  4. Add a text box to the form and set the following properties for the text box:


  5. 
       Name: ElapsedTime
       DefaultValue: "00:00:00:00"
       Enabled: No
       Locked: Yes 
  6. Add a command button to the form and set the following properties for the command button:


  7. 
       Name: btnStartStop
       Caption: Start
       OnClick: [Event Procedure] 
  8. Add a second command button to the form and set the following properties for the second command button:


  9. 
       Name: btnReset
       Caption: Reset
       OnClick: [Event Procedure] 
  10. Click Code on the View menu to open the editor. Type the following lines in the Declarations section:


  11. 
    Option Explicit
    Dim TotalElapsedMilliSec As Long
    Dim StartTickCount As Long
    Private Declare Function GetTickCount Lib "kernel32" () As Long 
  12. Set the OnTimer property of the form to the following event procedure:


  13. 
    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 
  14. Set the OnClick property of the btnStartStop command button to the following event procedure:


  15. 
    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
     
  16. Set the OnClick property of the btnReset command button to the following event procedure:


  17. 
    Private Sub btnReset_Click()
       TotalElapsedMilliSec = 0
       Me!ElapsedTime = "00:00:00:00"
    End Sub 
  18. Open the form in Form view to test the stop watch.


Additional query words:


Keywords          : kbdta FmsHowto 
Version           : WINDOWS:2000
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: July 6, 1999