Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / March 2007

Tip: Looking for answers? Try searching our database.

Me

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rod - 06 Mar 2007 19:32 GMT
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
Douglas J. Steele - 06 Mar 2007 19:42 GMT
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.

Signature

Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

> 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?
[quoted text clipped - 9 lines]
>     CallToFunction(ThisButtonName)
> end sub
Rod - 06 Mar 2007 20:42 GMT
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.
Douglas J. Steele - 06 Mar 2007 23:31 GMT
John's suggestion of using Screen.ActiveControl is probably your best way
then.

Another alternative would be:

Button_A_Click()
    Function(Screen.ActiveControl.Name)
End Sub

Signature

Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no private e-mails, please)

>I don't want to have to add the name of the button to the code if I can
>avoid
[quoted text clipped - 24 lines]
> to
> have to go in and add all of their names.
John Spencer - 06 Mar 2007 20:05 GMT
Screen.ActiveControl  may do what you want

Public Function CallToFunction()
Dim ctlAny as Control

  Set CtlAny = Screen.ActiveControl

  Debug.Print CtlAny.Name

End Function

Signature

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

> 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?
[quoted text clipped - 9 lines]
>     CallToFunction(ThisButtonName)
> end sub
Rod - 07 Mar 2007 14:24 GMT
Amazing! Thanks!

> Screen.ActiveControl  may do what you want
>
[quoted text clipped - 20 lines]
> >     CallToFunction(ThisButtonName)
> > end sub
Rick Brandt - 07 Mar 2007 00:01 GMT
> 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?
[quoted text clipped - 8 lines]
>     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.

Signature

Rick Brandt, Microsoft Access MVP
Email (as appropriate) to...
RBrandt   at   Hunter   dot   com

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.