Me

R

Rod

You know how you can use the "Me" keyword to refer to the current form? Is
there a way to do the same thing with a button?

I have a bunch of buttons and I want to pass their names to a function when
they're pressed. I don't want to have to modify each of their on_click
statements with their name but rather keep them all the same and just pass
the name to the function.

e.g.
Button_A_On_Click()
CallToFunction(ThisButtonName)
end sub
 
D

Douglas J. Steele

If you declare the function as

Function CallToFunction(NameOfButton As String)

and pass the name as a string:

Button_A_On_Click()
CallToFunction("ThisButtonName")
End Sub

you can then use

Me.Controls(NameOfButton)

in the function (assuming it's in the class module associated with the form,
as opposed to a common module)

Alternatively, you can declare the function as

Function CallToFunction(Button As Control)

and pass a reference to the button:

Button_A_On_Click()
CallToFunction(Me!ThisButtonName)
End Sub

and then simply refer to Button in your function.
 
J

John Spencer

Screen.ActiveControl may do what you want

Public Function CallToFunction()
Dim ctlAny as Control

Set CtlAny = Screen.ActiveControl

Debug.Print CtlAny.Name

End Function


--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..
 
R

Rod

I don't want to have to add the name of the button to the code if I can avoid
it. This is what I'm doing now:

Button_A_Click()
Function("Button_A")
End Sub
Button_B_Click()
Function("Button_B")
End Sub
Button_C_Click()
Function("Button_C")
End Sub

This is what I'm trying to get to but don't know if it's possible:
Button_A_Click()
Function(ThisObject.Name)
End Sub
Button_B_Click()
Function(ThisObject.Name)
End Sub
Button_C_Click()
Function(ThisObject.Name)
End Sub

So that all the code is the same. There are so many buttons I don't want to
have to go in and add all of their names.
 
R

Rick Brandt

Rod said:
You know how you can use the "Me" keyword to refer to the current
form? Is there a way to do the same thing with a button?

I have a bunch of buttons and I want to pass their names to a
function when they're pressed. I don't want to have to modify each of
their on_click statements with their name but rather keep them all
the same and just pass the name to the function.

e.g.
Button_A_On_Click()
CallToFunction(ThisButtonName)
end sub

I suspect you're thinking of the way that you can call a javascript function in
a web page like...

onclick=SomeFunction(this)

....and a reference to the "thing" that was clicked on is automatically passed to
the function. Access/VBA doesn't have an equivalent of that.
Screen.ActiveControl is about as close as you will get.
 
R

Rod

Amazing! Thanks!

John Spencer said:
Screen.ActiveControl may do what you want

Public Function CallToFunction()
Dim ctlAny as Control

Set CtlAny = Screen.ActiveControl

Debug.Print CtlAny.Name

End Function


--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..
 

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