Prevent two fast single clicks in succession on a from button - prevent code being run twice by accident (1 Viewer)

bignose2

Registered User.
Local time
Today, 19:34
Joined
May 2, 2010
Messages
197
Hi,
I have a button that sends an email, via MS Schemas, OLE etc.
Run on a button single press
If I accidently press this twice, almost like a double click, it runs the code twice & freezes & sort of crashes, recovers usually but 30 seconds.

I wondered if there was a simple, elegant was to stop this,
I guess a timer or something, or a flag to make sure message has finished sending but with this sort of thing going on in the background of windows not sure that will help much.

To be honest never really been a problem but I discovered this issues with a faulty mouse, I thought my code or installation had gone buggy, spend ages trying to fix then noticed what I thought was the mouse button releasing early, was actually another click, without pressing the button.

Anyway, new mouse but for some bizarre reason I now find myself clicking twice by accident. I guess a new mouse, Even if I change mouse seemed like a good idea to try to avoid all together for me or my staff.
 

Gasman

Enthusiastic Amateur
Local time
Today, 19:34
Joined
Sep 21, 2011
Messages
9,025
Disable the button as your first line of code, enable as your last in that event?

You might be suffering from the logitech double click mouse syndrome?, mine does it once is a while :(
 

isladogs

CID VIP
Local time
Today, 19:34
Joined
Jan 14, 2017
Messages
15,392
Change it to a double click event? 😉
 

theDBguy

I’m here to help
Staff member
Local time
Today, 11:34
Joined
Oct 29, 2018
Messages
16,519
Not sure you can disable a control while it has the focus, so you may have to send the focus elsewhere first.

Another possibility is to store the date and time the last email was sent, so the code won't run if it's not time yet to send another email.
 

sxschech

Registered User.
Local time
Today, 11:34
Joined
Mar 2, 2010
Messages
717
Assuming that the action to take place happens only one time, that is to say you would not be needing to redo it while you are on the same record...
What about putting a hidden text field on the form. When you click the button, have the code put 1 in the text box then you can test if the value of the text box = 1, don't run the code. Once you move on to another record, reset the textbox to 0, that way it is ready to be used again on a different record or if you need to redo the original record, moving to a different record and back will then allow the code to be run again.

Something like:
Code:
Private Sub Command4_Click()
    If Me.Text2 = 0 Then
        Me.Text2 = 1
        MsgBox "Code Runs"
        'Rest of your code goes here
        
    End If
End Sub

Private Sub Form_Current()
    Me.Text2 = 0
End Sub
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 14:34
Joined
Feb 19, 2002
Messages
33,168
When I send emails, I log the event. You could use that log to determine that the second send is a duplicate.
 
Last edited:

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 13:34
Joined
Feb 28, 2001
Messages
20,831
First, concur with the idea that you cannot disable a control with focus.

Second, I have a question about the sequence of events. An event cannot interrupt another event, so either you honored the CLICK event (because it was the first click in sequence) or you queued up a second CLICK event (because you can't interrupt the first click.) So the problem isn't REALLY in the click, it is that you didn't wait for the send-mail action to finish. I would suggest looking at the documentation for the utility you are activating to see if there is a "READY" flag to test.
 

MarkK

bit cruncher
Local time
Today, 11:34
Joined
Mar 17, 2004
Messages
7,829
You can also programmatically redirect the second click to a different handler, like...
Code:
Private Sub Command0_Click()
    Me.Command0.OnClick = "=NotReady()"
    Me.TimerInterval = 1500
    ' for the next 1.5 seconds, this button is not ready
End Sub

Function NotReady()
    MsgBox "The button is not ready"
End Function

Private Sub Form_Timer()
    Me.TimerInterval = 0
    Me.Command0.OnClick = "[Event Procedure]"
End Sub
 

Users who are viewing this thread

Top Bottom