Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Press and hold to Repeat
Jeremy Singh 
    
7 months ago
For a simple button that increments a counter on a quantity field, is it possible to make the button to continuously fire the increment code if the user holds down left-click on the button?

I tried ChatGPT, which gave me a solution using Timer Interval which looks like it should work but it doesn't for me.
ChatGPT's solution:

Private Sub Form_Load()
    Me.TimerInterval = 0  ' Timer is off by default
    Me.txtCounter = 0     ' Start counter at 0
End Sub


Private Sub cmdIncrement_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Me.TimerInterval = 200   ' Timer fires every 200 ms (adjust speed as needed)
End Sub


Private Sub cmdIncrement_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Me.TimerInterval = 0     ' Stop timer when mouse is released
End Sub


Private Sub Form_Timer()
    ' Increment the counter while button is held
    Me.txtCounter = Nz(Me.txtCounter, 0) + 1
End Sub


Is this possible or should I just forget about it?
Donald Blackwell  @Reply  
        
7 months ago
Jeremy

The reason it doesn't work is that immediately after the mousedown event fires and runs its code the click event fires and takes away the control

I tried a few different things and couldn't get it to increment by more than 1 on the mouse down event...

However, I was able to get it to go using a slightly altered method:

DetailsOption Explicit
Dim Increment as Boolean

Private Sub cmdIncrement_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

     Increment = False
     If Shift = 1 Then Increment = True ' Hold the Shift key when clicking the button to activate auto-increment

End Sub

Private Sub cmdIncrement_Click()

     If Increment Then ' If the Auto-Increment has been enabled, then start the timer
          TimerInterval = 200
     Else ' If the Auto-Increment isn't set, then increment by one and stop
          Form_Timer
          TimerInterval = 0
     End If

End Sub


With this method, hold the shift key while clicking and the auto increment will work. Click again without holding shift to stop the auto increment.
Kevin Yip  @Reply  
     
7 months ago
One way to use only mouse-down, no Shift key, no form timer:

In my code below, when MouseDown event runs, it sets a global boolean variable to true, indicating the repeat action (in this case, incrementing a field) will start.  When MouseUp runs (user releases mouse button), the global boolean variable is set to false, stopping the repeat.

When the MouseDown runs, it starts a "repeat delay" -- the time that passes (half a sec or so) before the repeat action starts.  After the repeat delay, if the user is still pressing the button, the repeat action starts.  A "repeat speed" (CPU-dependent) is used to determine how fast the action goes.

The key is to use of DoEvents, which passes control back to Access, allow the coding to check for variable values.

In my sample form below, I use a button named IncrementBtn and a textbox named field1.
Kevin Yip  @Reply  
     
7 months ago

Donald Blackwell  @Reply  
        
7 months ago
Jeremy

Found an even easier way without using global variables or the timer interval.

Details

Private Sub cmdIncrement_Click()

     txtCounter = Nz(txtCounter) + 1
     txtCounter.setFocus
     cmdIncrement.setFocus
     Sleep 200

End Sub



Then, in the "Other" tab of the cmdIncrement button's property dialog, set "Auto Repeat" to "Yes"

If txtCounter is null, the Nz function sets it to 0 by default and add one.

Next, briefly move the focus to the txtCounter textbox so that you can see the increment, otherwise, it will increment but you won't see it until you let go of the button.

Then focus moves back to the cmdIncrement button and then waits 200 milliseconds (just like the timer interval would do) and then, if you're still holding the button, it repeats the process.

You will need to make sure you have the following in a global module, if you don't already:

Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal Milliseconds As LongPtr)
Kevin Yip  @Reply  
     
7 months ago
AutoRepeat doesn't let you customize the repeat delay or repeat speed.  They are fixed at 0.5 and 0.25 sec, which are too fast for me.  If the user doesn't mind the fixed speed, this is a nice and simple method.  DoEvents would also pass control back to Access, so the screen would refresh to show the increments.  P.S. Windows lets you set repeat delay and repeat speed (pic below), but only for keystrokes, not for mouse-clicks
Kevin Yip  @Reply  
     
7 months ago

Donald Blackwell  @Reply  
        
7 months ago
Kevin Yeah, but the sleep duration basically handles the autorepeat delay because you get how ever long you specify to just take your finger off the button and you can fine tune before release or set an editable field in a table with various pre-sets that the end user could select from.

I did try a DoEvents and it didn't refresh the screen without the focus manipulation but probably wouldn't be bad to throw in to allow other events to process.

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 5/1/2026 10:35:02 PM. PLT: 1s