By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,461 Members | 2,184 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,461 IT Pros & Developers. It's quick & easy.

Use a variable to reference a command button

P: 2
I want to click a command button in code. In place of this: CommandButton1_click, I would like to use a variable like this: Variable_click. What is the syntax for this?
Mar 1 '17 #1
Share this Question
Share on Google+
2 Replies

Expert 100+
P: 1,221
You'll need more than syntax. You'll have to decide on an approach and code the various functions. You might try the Form's On_Click event. In that event you can test the name of the current object (what they clicked on) and then run the code appropriate to that object (Screen.ActiveControl).

Normally that would just be a lot more trouble than using each object's click event. But your situation may demand a more complicated soltution. Tell us more about what you are trying to accomplish. Perhaps there is a better approach.

Mar 1 '17 #2

P: 2
Looping thru a bunch of cmdbuttons and clicking each if a checkbox next to it is checked. I worked it out a diff way, like this:
'process all boxes
For Each Ctrl In form_mainform.Controls
If TypeName(Ctrl) = "CheckBox" And Ctrl.Name Like "*" & AA & "*" Then
If Ctrl.Value = -1 Then
theState = Mid(Ctrl.Name, 4, 2)
theCmd = "cmd" & theState

Select Case theCmd

Case "cmdaz"

Case "cmdde"

Case "cmdfl"
Instead of calling each button by name separately, I was trying to use something like: Form_MainForm.controls(theCmd)_click in the loop. But what I did works. Still open to suggestions though. Thx.
Mar 1 '17 #3

Post your reply

Sign in to post your reply or Sign up for a free account.