Getting The Name Of a Pressed Command Button

striker

Useless and getting worse
Local time
Today, 09:24
Joined
Apr 4, 2002
Messages
65
I have a form which has 22 command buttons on it, I have written a procedure which formats the command button when it is clicked, but the way i see it at the momment I have to write 22 slightly different procedures i.e one for each of the buttons.

What I want to do is write a procedure which can identify which button(s) have been clicked. I did think of storing the button name in an array but i can't seem to think of a way of collecting the button name.

its this statment from the procedure below that I want to get rid of or change just so that I don't have 22 slightly different procedures.

Var_Which_Btn_Pressed = "btn_Aircraft_Selector_Plane01"

The procedure for one button is shown in total below

Thanks for any input you can give.


Private Sub btn_Aircraft_Selector_Plane01_Click()

Var_Which_Btn_Pressed = "btn_Aircraft_Selector_Plane01"

Last_Two = Right(Var_Which_Btn_Pressed, 2)

If Me(Var_Which_Btn_Pressed).Caption = "C" Then

Array_Which_Button_Pressed(Last_Two) = ""

format_btn_selected

ElseIf Me(Var_Which_Btn_Pressed).Caption = "D" Then

Array_Which_Button_Pressed(Last_Two) = Right(Var_Which_Btn_Pressed, 5)

format_btn_not_selected

End If


End Sub
 
You can cycle through the forms container to collect the names of each type of control.
 
Sorry, I'm not sure what you mean?
 
dim ctl as control
for each ctl in me
if ctl.controltype = accombobox then
name = ctl.name
end if
next


Gives you all the combo box control names.
 
Not sure how to apply that

llkhoutx,

Thanks for the snippet, but i'm not sure how to apply it in this case, what i'm trying to do is get the identity of the button as it is clicked.

Having run the snippet of code it just seems to cycle through all the command buttons on the form.

Am I having a particularly dense moment and can't see the point or have I just lost it after all.

Thanks again.
 
Striker,

I have no idea how you would do that!

But, here is a method that will require less code then what you are using.
Code:
Private Sub cmdButton1_Click()
    Dim ctl As Control
    Set ctl = cmdButton1
    formatButton ctl
End Sub

Public Sub formatButton (myCtl As Control)
    With myCtl
        If .Tag = "Format1" Then
         .Caption = "Hello"
         .ForeColor = vbBlue
        ElseIf .Tag = "Format2" Then
         .Caption = "Goodbye"
         .ForeColor = vbRed
        End If
    End With
End Sub
You will still need to have code for each of the command buttons, but not as much as before.

Hope this helps a bit

Dave
 
AAAAAAAh

dgm,

Thanks for that I've since tidied up my original code but without the ability to know which button has been clicked I'm still going to have to duplicate the whole procedure 22 times, which to be honest seems a bit excessive, especially as I can change the names of the buttons in code.

All I want is to be able to interrogate the Name property of the clicked button, I can't believe its that difficult.

Thanx for the code anyway

Any other experts got a clue?
 
You say you have twenty-two buttons. The only thing you would need to repeat twenty two times is the call to your function from each button:

Private Sub btn_Aircraft_Selector_Plane01_Click()
whichButton btn_Aircraft_Selector_Plane01
End Sub

Private Sub btn_Aircraft_Selector_Plane02_Click()
whichButton btn_Aircraft_Selector_Plane02
End Sub

Private Sub btn_Aircraft_Selector_Plane03_Click()
whichButton btn_Aircraft_Selector_Plane03
End Sub

Private Sub btn_Aircraft_Selector_Plane04_Click()
whichButton btn_Aircraft_Selector_Plane04
End Sub


Sub whichButton(ByRef cntrlButton As Button)
Dim strName as String
Dim Last_Two as Long

strName = cntrlButton.Name

Last_Two = Right(strName, 2)

If cntrlButton.Caption = "C" Then

Array_Which_Button_Pressed(Last_Two) = ""

format_btn_selected

ElseIf cntrlButton.Caption = "D" Then

Array_Which_Button_Pressed(Last_Two) = Right(strName, 5)

format_btn_not_selected

End If

End Sub


Basically what this does is passes the button you just pressed to your Sub it can then use or change any of the button's properties.

I don't know if this particular code works, I just changed the information from your code to fit this new codes variables. I just wanted you to get the general idea of how it would work.

Does this help?

Peace
 
Drevlin,

Thanx for that but I've tried it and I get an error message on the following line "User defined type not defined" which has now completely thrown me

Sub whichButton(ByRef cntrlButton As Button)

Any ideas

thanx again
 
I'm sorry I was being stupid. It's CommandButton not Button. Or you could use Control as well.
 
I now get the error "ByRef argument type mismatch" this appears at the line

whichButton btn_Aircraft_Selector_Plane01

I'm not sure I wholly understand whats going on here

Thanx again
 
I think changing "as CommandButton" to "As Control" should get it to work.

I'll explain what we're trying to do though, so you can undestand it and reuse it in the future if necessary. (or to give someone the chance to correct me if I misunderstand something)

Ok, simply put we are having the command button pass itself to your Sub proceedure. The Sub then takes your command button and performs operations on it.

Much like if you have the number 10 you can pass that number to a variable:

Dim lngNum as long
lngNum = 10

You declare the variable type and pass the variable to it.

You do the same thing with objects.

Dim cntrl as Control
cntrl = Me.txtBox

if you then print the value of cntrl it will be whatever is inside of txtBox if me.txtBox = "Hello" the following:

Debug.Print cntrl.Value

will print: Hello

The exact same thing happens when you pass a control to another sub:

Sub txtBox_AfterUpdate()
whichControl txtBox
End Sub

Sub whichControl(cntrl as Control)
Debug.Print cntrl.Value
Debug.Print cntrl.Caption
End Sub

This would print:
Hello
txtBox

The Variable cntrl in the sub whichControl has all the same properties as txtBox.

Does this make sense?
 
Drevlin,

Thank you for the explanation and yes I know have a vague understanding of what is going on.

OK that now seems to do as I envisaged on button plane01 but it doesn't work for button 2 onwards and if I click button 2 it changes the state of button 1

I'm rapidly looseing the will to live over this one but I will not give in now.

Any ideas cause I'm now baffled ?
 
Could you post your code for button2? You need to make sure that you are passing the button that is pressed:

Private Sub btn_Aircraft_Selector_Plane02_Click()
whichButton btn_Aircraft_Selector_Plane02
End Sub

It sounds like you have this:

Private Sub btn_Aircraft_Selector_Plane02_Click()
whichButton btn_Aircraft_Selector_Plane01
End Sub
 
Drevlin,

Thanks for all your help, it was a spelling mistake that was the problem. its difficult to type using my elbows having bitten my finger nails that far.

Once again thanks for the patience and help. Ive got over the hurdle and heading for the line.
 
Pat,

Will that work in access97, I cant check until monday cause I've only got 2000 on my machine at home and i'm not in the office till monday and if its not being too cheeky an example of how to apply would be great.

thanks.
 
Seems to me it wolud have been quicker to write 22 procedures than the to and fro here:D

The most used keys on my keyboard are ctrl C and ctrl V;)

I know how it gets you in. In the end you want to prove to yourself it can be done.

Good luck
Dave
 
Code:
Private Sub cmdButton01_Click()
    Call formatButton(ActiveControl)
End Sub

Private Sub cmdButton02_Click()
    Call formatButton(ActiveControl)
End Sub

Public Sub formatButton (myCtl As Control)

   Last_Two = Right(myCtl.Name, 2) 

   If myCtl.Caption = "C" Then 
      Array_Which_Button_Pressed(Last_Two) = "" 
      format_btn_selected 
   ElseIf myCtl.Caption = "D" Then 
      Array_Which_Button_Pressed(Last_Two) = Right   
      format_btn_not_selected 
   End If 


End Sub
 
Thanks to all those who have offered advice and suggested a way of doing this i've now sussed it out and it works.
 
You can use the screen.activecontrol property to return the name of the selected command button. I have used this in a form with multiple buttons, assigned a tag to each button, and called a common function and processed based on the tag - nice and clean using a Select case structure.

Have included the help text below:

---------------------------------------------------------------------------------
ActiveControl Property


You can use the ActiveControl property together with the Screen object to identify or refer to the control that has the focus.

Setting

This property setting contains a reference to the Control object that has the focus at run time.

This property is available by using a macro or Visual Basic and is read-only in all views.

Remarks

You can use the ActiveControl property to refer to the control that has the focus at run time together with one of its properties or methods. The following example assigns the name of the control with the focus to the strControlName variable:

Dim ctlCurrentControl As Control
Dim strControlName As String
Set ctlCurrentControl = Screen.ActiveControl
strControlName = ctlCurrentControl.NameIf no control has the focus when you use the ActiveControl property, or if all of the active form's controls are hidden or disabled, an error occurs.

-------------------------------------------------------------------------
 

Users who are viewing this thread

Back
Top Bottom