ACC1x: How to Implement a Timer Within MS Access Applications

ID: Q95924


The information in this article applies to:


SUMMARY

There is no timer control built into Microsoft Access version 1.x. To implement a timer, you must create an Access Basic module that creates a timing sequence in your application.


MORE INFORMATION

To create a timer within your Microsoft Access application, you must call two functions. One function starts the sequence, and another function sets a global variable that tells the first function to stop performing the timing iteration.

This article assumes you are familiar with Access Basic and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information on Access Basic, please refer to the "Introduction to Programming" manual.

In the first function, called StartTimer(), you execute an endless loop that performs the timing update and the DoEvents() function. The DoEvents() function instructs the Access Basic function to release the computer's CPU to other tasks, such as your form, other applications within Microsoft Windows, and most importantly the StopTimer() function. The StopTimer() function sets the global variable that the StartTimer() function checks to determine when to stop looping.

To create an example, follow the instructions below. Once these are done, open the form in Browse mode and press the Start Timer button. You should see the timing being performed by the updating in the text box. At any point you can stop the timing sequence by pressing the Stop Timer button.

  1. Create the following controls on a form and set the appropriate properties:
    
          Form: Form1
          ------------------------
          Control: Text Box
             ControlName: Field0
          Control: Command Button
             Caption: Start Timer
             OnPush: =StartTimer()
          Control: Command Button
             Caption: Stop Timer
             OnPush: =StopTimer() 


  2. Create a new module and add the following code:
    
    
          '---------------------------------------
          'GLOBAL DECLARATIONS SECTION
          '---------------------------------------
          Option Explicit
          Dim StopTheTimer     'if set to TRUE, then exit StartTimer()
          Const INTERVAL = 1   'determine how often in seconds to update
                               'the screen
    
          '---------------------------------------------------
          'This function will start the timer, update
          'the display with the correct time unit,
          'and look to see if the StopTheTimer is
          'set to TRUE so as to determine when to
          'stop the process.
          '---------------------------------------------------
          Function StartTimer ()
             Static InHere  'stop re-entrant code
             Dim iLast, iNow, x%
    
             'this stops the user from pressing the command button
             'and re-entering this code again if it is already
             'running.
             If InHere = True Then Exit Function
    
             iLast = Timer
             Do
                'StopTheTimer is set in StopTimer()
                If StopTheTimer = True Then
                   StopTheTimer = False
                   Exit Function
                End If
    
                iNow = Timer
    
                'need to update display only if the interval
                'has been exceeded.
                If (iNow - iLast) > INTERVAL Then
                   Forms!Form1!Field0 = iNow
                   iLast = iNow
                End If
    
                'release the CPU to other tasks, namely enable
                'the user to enter the StopTimer() function.
                x% = DoEvents()
    
             Loop
          End Function
    
          '---------------------------------------------------
          'This function will set the flag to let StartTimer()
          'stop execution.
          '---------------------------------------------------
          Function StopTimer ()
             StopTheTimer = True
          End Function 



Keywords          : kbprg 
Version           : 1.0 1.1
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: March 18, 1999