Call event procedure identified by variable

Mark L
3StarLounger
Posts: 331
Joined: 11 Feb 2010, 03:55
Location: Land O Lakes, FL

Call event procedure identified by variable

Post by Mark L »

Let's suppose I have many command buttons on a form, each with code in it's click event. Based on some other user activity from which I identify a specific button by name, I want to be able to then "click" that button. That is, I want to execute the click event of the button identified in strButtonName.
Last edited by Mark L on 01 Aug 2014, 15:42, edited 2 times in total.
Mark Liquorman
Land O Lakes, FL
see my website http://www.liquorman.net for Access Tips and Tricks, and for my Liquorman Utilities.

User avatar
burrina
4StarLounger
Posts: 550
Joined: 30 Jul 2014, 23:58

Re: Call even procedure with parameter

Post by burrina »

If I understand correctly you want to click a command button without actually clicking it!
If this is true, then try this. Of course change cmdLogin to your command button name.

Code: Select all

'See if the ENTER Key was hit during Password entry.
    'If it was then...
     
    If KeyCode = 13 Then
       'Fire the cmdLogin_Click procedure
       Call cmdLogin_Click
       'Null the ENTER Keystroke.
       KeyCode = 0
    End If

HTH

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Call even procedure with parameter

Post by HansV »

By default, event procedures are declared as Private. You will have to change them all to Public.
You can then use

Code: Select all

    CallByName Me, strButtonName & "_Click", VbMethod
assuming that you run the code from the form itself. If not, change Me to Forms("NameOfYourForm").
Best wishes,
Hans

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Call even procedure with parameter

Post by HansV »

burrina wrote:(...) Of course change cmdLogin to your command button name. (...)
Welcome to Eileen's Lounge!

Mark wants to provide the control name as a string, so we can't use Call cmdLogin_Click - that doesn't accept a string as argument.
Best wishes,
Hans

Mark L
3StarLounger
Posts: 331
Joined: 11 Feb 2010, 03:55
Location: Land O Lakes, FL

Re: Call event procedure identified by variable

Post by Mark L »

Hans,

Thanks! That looks exactly like what I was looking for. I had never heard of the CallByName function. Is this new? Was it in Access2003?
Mark Liquorman
Land O Lakes, FL
see my website http://www.liquorman.net for Access Tips and Tricks, and for my Liquorman Utilities.

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Call event procedure identified by variable

Post by HansV »

Hi Mark,

It's a generic VBA function introduced in Office 2000.
Best wishes,
Hans

Mark L
3StarLounger
Posts: 331
Joined: 11 Feb 2010, 03:55
Location: Land O Lakes, FL

Re: Call event procedure identified by variable

Post by Mark L »

HansV wrote:Hi Mark,

It's a generic VBA function introduced in Office 2000.
Great. The particular situation required compatibility as far back as 2003.

I had tried everything I could think of. I even defined an object in code and then used: set obj = me(strButtonName)
I could see that obj.onclick was a [event procedure], but obj_click gave an error. I was reduced to setting the focus to the control in strbuttonName, then using SendKeys "{ENTER}", which I really didn't want to do!

Thanks again.
Mark Liquorman
Land O Lakes, FL
see my website http://www.liquorman.net for Access Tips and Tricks, and for my Liquorman Utilities.

BenCasey
4StarLounger
Posts: 495
Joined: 13 Sep 2013, 07:56

Re: Call event procedure identified by variable

Post by BenCasey »

Mark, another way is to have the button on_click event call a public procedure and then have the code that you want call the same public procedure. You can even parametrise the public procedure to have it tell you when it was called from and the, if you wish, carry out additional work based on that.
eg.

Code: Select all

Public function pfSomething (frmName as string = "") as long 
' if you dont want it to return a value, of whatever  type is useful, then made it a sub
if frmname = "" then ' not called from form button
 ' whatever
 pfSomething=1
else
 'whatever
 pfSomething=0
endif
end function
Then:
Call pfSomething(me.name) ' calling from the form click
or
Call psSomething() ' not calling from the form click
Regards, Ben

"Science is the belief in the ignorance of the experts."
- Richard Feynman