Tick Counter

Introduction

The Win32 library provides a special function used to count a specific number of lapses that have occurred since you started your computer. This information or counter is available through the GetTickCount() function. Its syntax is:

DWORD GetTickCount(VOID);

 

This function takes no argument. If it succeeds in performing its operation, which it usually does, it provides the number of milliseconds that have elapsed since you started your computer. Just like the timer control, what you do with the result of this function is up to you and it can be used in various circumstances. For example, computer games and simulations make great use of this function.

After retrieving the value that this function provides, you can display it in a text-based control.

 

Practical LearningPractical Learning:  Using GetTickCount

  1. Start Microsoft Access and create a blank database named Computer Time
  2. Start a new form in Design View. Save it as TickCounter and design it as follows
     
    Control Properties
    Form Caption: Tick Counter
    Data Entry: No
    Record Selectors: No
    Navigation Buttons: No
    Modal: Yes
    Border Style: Dialog
    Timer Interval: 20
    Text Box Name: txtAppTime
    Back Color: -2147483633 (I am using the color of Windows XP. The idea is to hide the background of the text box. Therefore, if you are using another operating system such as Windows 9X or Windows Server 2003, because they use a different color, select it accordingly)
    Special Effect: Flat
    Border Style: Transparent
    Text Box Name: txtCompTime
    Back Color: -2147483633 (same reason as above)
    Special Effect: Flat
    Border Style: Transparent
  3. Initiate the OnLoad and the OnTimer events of the form
  4. Implement the form module as follows:
     
    Option Compare Database
    Option Explicit
    Private Declare Function Win32GetTickCounter Lib "Kernel32" _
    		Alias "GetTickCount" () As Long
    Private CompTime As Long
    
    Private Sub Form_Load()
        CompTime = Win32GetTickCounter
    End Sub
    
    Private Sub Form_Timer()
        Dim TickValue  As Long
        Dim Difference As Long
        
        TickValue = Win32GetTickCounter
        Difference = TickValue - CompTime
        
        Dim ComputerHours As Long
        Dim ComputerMinutes As Long
        Dim ComputerSeconds As Long
        Dim ApplicationHours, ApplicationMinutes, ApplicationSeconds As Long
    
        ComputerHours = (TickValue \ 3596400) Mod 24
        ComputerMinutes = (TickValue \ 59940) Mod 60
        ComputerSeconds = (TickValue \ 999) Mod 60
        ApplicationHours = (Difference \ 3596400) Mod 24
        ApplicationMinutes = (Difference \ 59940) Mod 60
        ApplicationSeconds = (Difference \ 999) Mod 60
        
        txtAppTime = "This application has been ON for " & _
                         CStr(ApplicationHours) & " hours, " & _
                         CStr(ApplicationMinutes) & " minutes, and " & _
                         CStr(ApplicationSeconds) & " seconds"
        
        txtCompTime = "This computer has been on for " & _
                         CStr(ComputerHours) & " hours, " & _
                         CStr(ComputerMinutes) & " mnutes, and " & _
                         CStr(ComputerSeconds) & " seconds"
    End Sub
  5. Return to Microsoft Access
  6. Switch the form to Form View to see the result
 

Copyright 2004-2005 FunctionX, Inc. FunctionX