Button Control Click can fire twice (1 Viewer)

ASherbuck

Registered User.
Local time
Today, 09:21
Joined
Feb 25, 2008
Messages
194
I've noticed in the past that if I have a button control on a form and have an code like such behind it:

Code:
Private Sub BtnSaveClose_Click()

currentdb.execute "INSERT INTO tblTable.* FROM tblTable.*;"

DoCmd.Close frmForm

end sub

If I double click, or just click fast enough I can fire off that event before the form closes, thereby running the query twice. Is it a better practice to structure my code as such:


Code:
Private Sub BtnSaveClose_Click()
 
DoCmd.Close frmForm

currentdb.execute "INSERT INTO tblTable.* FROM tblTable.*;"

end sub

The form control I am using is saving sale records and then resetting the form so it is not identical to the above, but similar enough for it to get the point across.
 

SOS

Registered Lunatic
Local time
Today, 09:21
Joined
Aug 27, 2008
Messages
3,517
One other way would be:

Code:
Private Sub BtnSaveClose_Click()
Static blnExecuted As Boolean
 
If blnExecuted = False Then
   blnExecuted = True
   currentdb.execute "INSERT INTO tblTable.* FROM tblTable.*;"
   DoCmd.Close frmForm
End If

end sub

And then it shouldn't matter as it would have set the variable to true already and so would just bypass it on the second pass. The form would close and therefore reset the static variable.
 

ASherbuck

Registered User.
Local time
Today, 09:21
Joined
Feb 25, 2008
Messages
194
I dig it, thanks man
 

Users who are viewing this thread

Top Bottom