Mouse Click Counter On Access Form

This post will demonstrate how you can count the number of clicks
on your button in a certain time frame.

It will function like a game .

Here is the database form all in one:

Option Compare Database

Public m_dteStartTime As Date
Public m_dteStopTime As Date

Private Sub btnClicker_Click()
    Dim intValue As Integer
    Dim rst As Recordset
    
    'read, write
    Set rst = CurrentDb.OpenRecordset("tblCurrentCount", dbOpenDynaset)
    
    intValue = DLookup("[cnt_current_count]", "tblCurrentCount")
    
    intValue = intValue + 1
    rst.Edit
    rst.Fields("cnt_current_count") = intValue
    rst.Update
    
    rst.Close
    Set rst = Nothing
    
    
    Me.lblCount.Caption = "The current count is " & intValue
    DoEvents
    
End Sub



Private Sub btnRestartCounter_Click()

    Dim rst As Recordset
    
    Set rst = CurrentDb.OpenRecordset("tblCurrentCount", dbOpenDynaset)
    
    rst.Edit
    rst.Fields("cnt_current_count") = 0
    rst.Update
    
    rst.Close
    Set rst = Nothing

    Me.lblCount.Caption = "The current count is 0"
    Me.lblResult.Caption = "Ready..."
    DoEvents
    
    'reset the modular variables
    m_dteStartTime = 0
    m_dteStopTime = 0
    
End Sub

Private Sub btnStart_Click()
    Me.TimerInterval = 1000
    m_dteStartTime = Now()
End Sub

Private Sub btnStop_Click()
    Dim intResult As Integer
    Dim intElapsedTime As Integer

    Me.TimerInterval = 0
    
    m_dteStopTime = Now()

    intElapsedTime = Format(m_dteStopTime - m_dteStartTime, "ss")
    
    'get the current count
    intValue = DLookup("[cnt_current_count]", "tblCurrentCount")
    
    Me.lblResult.Caption = intValue & " clicks in " & intElapsedTime & " seconds"
    DoEvents
    
    
End Sub

Private Sub Form_Timer()
    Me.lblTimer.Caption = Now()
    DoEvents
    
End Sub

Here is the database for an example.

mouse click counter.accdb

Watch how it’s done:






By the way, if you got or are getting value from the VBA information, please click the "Donate" button to give me a small token of your appreciation, thanks!


These posts may help answer your question too...

Learn Access VBA: Understand Tables, Queries, Forms, and Reports

Learn Access VBA: From Zero to Database Hero If you’ve ever opened Microsoft Access and wondered how all the pieces fit together — tables, queries, forms, and reports — this tutorial is made for you. In just a few minutes, you’ll understand how Access works behind the scenes and see how VBA (Visual Basic for […]

How To Make An Access Form Time Picker

Here is a relatively easy way to select times for your time entry text boxes. It’s a reusable form that allows you to pick a time from an Access form. There are probably different ways to do this but here is the way I would do it. On the form that has the time fields, […]

How to pick a file to load In VBA

How to Pick a File in VBA: FileDialog & GetOpenFilename Explained When building Excel VBA applications, you’ll often need to let users pick a file to load in VBA. Instead of hard-coding file paths, you can use built-in dialogs that make file selection easy and user-friendly. VBA offers two main approaches: FileDialog object (flexible, customizable) […]

What is the purpose of the Me keyword in Access VBA?

What does the Me keyword mean? “Me” refers to the Access form currently in focus. Instead of writing out the entire form reference, you can just use the keyword “Me” which is easier. Like: Me.txtbox = “I am a textbox on the form that currently has the focus.” or you can update a label’s caption […]


Support these sponsors:
Previous Post

How To Generate A XML File With Access VBA

Next Post

Find Error Line Number in VBA With Erl