Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

ACC2000: How to Create a Stopwatch Form


View products that this article applies to.

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.

NOTE: This article explains a technique demonstrated in the sample file, FrmSmp00.mdb. For information about how to obtain this sample file, please see the following article in the Microsoft Knowledge Base:
233324 Microsoft Access 2000 Sample Forms Database Available in Download Center
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.

↑ Back to the top


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 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 "kernel32" () As Long
    					
  7. 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), "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. 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 = "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. Set the OnClick property of the btnReset command button to the following 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.

↑ Back to the top


Keywords: KB233275, kbhowto

↑ Back to the top

Article Info
Article ID : 233275
Revision : 3
Created on : 6/23/2005
Published on : 6/23/2005
Exists online : False
Views : 324