Is there a value associated with OnClick event of a command button?

  • Thread starter Thread starter tcb
  • Start date Start date
T

tcb

Is there a value associated with OnClick event of a command button? If
a user clicks a button I would like to grab that value for later use.
 
No, there is no 'value' associated with the event. But you can record the
fact that a user clicked a particular button by using a static variable or a
form-level or global variable. Use a static variable if you need to
determine whether the user has already clicked the button from code within
the Click event procedure. Use a form-level variable if you need to
determine whether the user has already clicked the button from code behind
the same form. Use a global variable if you need to determine this from code
in other forms or modules.

Declare a static variable within the event procedure using the Static
keyword. To declare a form-level variable, declare it in the Declarations
section of the form module, after the Option Compare Database and Option
Explicit statements but before any Sub or Function declarations. To declare
a global variable, declare it using the Public keyword in a standard module
(not a form, report, or other class module).

Here's an example using a static variable ...

Private Sub cmdTest_Click()

Static ClickCount As Long

ClickCount = ClickCount + 1
Me.cmdTest.Caption = "You've clicked me " & ClickCount & " time(s)"

End Sub

See the VBA help topics 'Understanding the Lifetime of Variables' and
'Understanding Scope and Visibility' for more information.
 
Thanks Brendan. That was of good value to me, and an especially good
lesson on the use of variables.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top