Get control name in click event procedure

O

Olive Music

I have a form with two dozen command buttons on it that will all perform a
similar routine. Each of the buttons click events will have its own
subroutine in the VBA code, and all the subroutines will call the same
function. To have that function perform logic specific to a given button, I'd
have to pass the name of the button to the function. I'd rather use a
variable, so that for each button click event subroutine that I create, all I
have to do is copy and paste the statement from another button's click event
without having to subsequently change the function argument to be the
button's name. (I know that sounds lazy, but two dozen is a lot, and anyway
I'm curious if it can be done.) An alternative would be to somehow use the
subroutine's name as the value (since the name of a command button's click
event contains the button's name). But I haven't found a way to do that
either.
 
M

Marshall Barton

Olive said:
I have a form with two dozen command buttons on it that will all perform a
similar routine. Each of the buttons click events will have its own
subroutine in the VBA code, and all the subroutines will call the same
function. To have that function perform logic specific to a given button, I'd
have to pass the name of the button to the function. I'd rather use a
variable, so that for each button click event subroutine that I create, all I
have to do is copy and paste the statement from another button's click event
without having to subsequently change the function argument to be the
button's name. (I know that sounds lazy, but two dozen is a lot, and anyway
I'm curious if it can be done.) An alternative would be to somehow use the
subroutine's name as the value (since the name of a command button's click
event contains the button's name). But I haven't found a way to do that
either.

Me.ActiveControl.Name
 
M

Marshall Barton

Marshall said:
Me.ActiveControl.Name

Whoops, wasn't done typing.

Me.ActiveControl.Name is more specific than the
Screen.ActiveControl.Name

I really wanted to point out that you can set the OnClick
property to call your general function. Use =functionname()
instead of [Event Procedure] to save you from having a
separate function for each button that doesn't do anything
beyond calling the general procedure.
 

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

Top