no second button click

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there anyway to make a button only work once and then recieve a message if
it is clicked again?
 
If it is clicked again ever? Or only if it is clicked again within the
current session?

For the former, you'd need to store a value somewhere to record the fact
that the button had been clicked, and look it up each time the form opens.
For the latter, you could use a static variable.
 
clicked again within the current session.

Brendan Reynolds said:
If it is clicked again ever? Or only if it is clicked again within the
current session?

For the former, you'd need to store a value somewhere to record the fact
that the button had been clicked, and look it up each time the form opens.
For the latter, you could use a static variable.
 
I think you need to set a global variable inside VBA and test it during your
onclick event.
 
In a standard module ...

Option Compare Database
Option Explicit

Public gClickedAlready As Boolean

In the form's module ...

Private Sub cmdClickOnce_Click()

Static ClickedAlready As Boolean

If ClickedAlready = True Then
MsgBox "You clicked me already since opening this form!"
Else
ClickedAlready = True
MsgBox "First time you clicked me since opening this form!"
End If

If gClickedAlready = True Then
MsgBox "You clicked me already since opening this MDB!"
Else
gClickedAlready = True
MsgBox "First time you clicked me since opening this MDB!"
End If

End Sub

The local variable 'ClickedAlready' will retain its value as long as the
form is open (because it is declared using the 'Static' keyword). The global
variable, 'gClickedAlready', defined in the standard module, will retain its
value when you close and re-open the form.

For more info, look for the VBA help topics 'Understanding the Lifetime of
Variables' and 'Understanding Scope and Visibility'.
 
Back
Top