Dynamically Call Event Handler From String (1 Viewer)

modest

Registered User.
Local time
Today, 17:05
Joined
Jan 4, 2005
Messages
1,220
For some reason I didn't think this was possible, but I didn't want to throw all my cards away. Let's say I have a button called cmdButton and I would like to call cmdButton_onClick(). Does anyone know a way to do this by using a string.

For instance,

Code:
Dim sCtrl As String
sCtrl = "Button"
Me.Controls("cmd" & sCtrl).OnClick

I've tried to use .ReplaceLine() in the CodePane for the ActiveModule, but that won't call the function.
 
Last edited:

modest

Registered User.
Local time
Today, 17:05
Joined
Jan 4, 2005
Messages
1,220
I've also tried
Code:
CallByName Me.Controls("cmd" & sCtrl), "Click", VbMethod

and
Code:
Set objButton = Me.Controls("cmd" & sCtrl)
CallByName objButton, "Click", VbMethod

as well as
Code:
CallByName objButton, "Value", VbLet, True
 
Last edited:

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 16:05
Joined
Feb 28, 2001
Messages
27,146
First, consider that all event modules are called in EVENT context, so when you try to call them directly, you already have a problem.

Second, the CORRECT way to do this is to turn it on its head. If the button does something you want to do more often than when a given button is pushed, make it a subroutine and call the subroutine from the event code. Don't try to call the event code from non-event code.
 

modest

Registered User.
Local time
Today, 17:05
Joined
Jan 4, 2005
Messages
1,220
Doc, I really thought you would trust me on what I was doing :) I guess I'll have to use an example to demonstrate the dilemma - this might be long.

Let's say I have a tabbed form and the tabs are broken out, let's say, by Coach and Athlete. Both tabs have Contact Information.

When the user double clicks the person's full name, which is shown in one textbox, the textbox becomes hidden and the parts of the user's name become visible. For instance, if a textbox reads "Mr. Modest Man" and is double clicked, it hides and three textboxes appear where the user can change the prefix, first or last part of the name. With the edit boxes appears a save/cancel button, which the user can force a save with - making the edit boxes disappear and the fullname textbox reappear.

However, let's say the user wants to edit the person's address, after first editing the Name textboxes, without saving or cancelling (leaving it still in edit mode). The address textbox set up much like the name, the full address in one textbox is hidden and can be edited by many textboxes.

Well double-clicking the full address textbox checks to see if the Name has been saved yet, if it hasn't, the user is asked to save the name by calling the cmdCoach_Name_Save_Click(). Well the process for coach and athlete is the same. So instead of using more than one sub, I'd rather do a callbyname "cmd" & sCoach_or_Athlete & "_Name_Save"

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

okay maybe the example isn't the best, because both Save buttons would call a routine, but this is still needed. let's not concentrate on "better ways" and focus on the problem at hand :)
 

boblarson

Smeghead
Local time
Today, 14:05
Joined
Jan 12, 2001
Messages
32,059
Okay, here's a suggestion -

Create a sub in a standard module for each command button (but don't call it from the button) that does all of the saving, create a separate one that calls the correct one using a select statement and pass parameters to it at the time you need. Since each command button can call the same exact sub, you can, with just calling that main sub and passing it the correct parameters, depending on which item was active, you could run the same code as if the button was pressed.

Just a suggestion.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 16:05
Joined
Feb 28, 2001
Messages
27,146
Don't forget that you can pass in a CONTROL as a parameter to a sub. Bob and I agree on this solution, I think.

Make a sub that accepts the required arguments including the control in question. When you call the sub, do the action you wanted. Anything can call that sub - including the event code.

Doc, I really thought you would trust me on what I was doing

Trust has nothing to do with this. VBA does something different for event entry and return (dismissal) than it does for "normal" subroutine entry and return. THAT is why you can't call the event code. But there is nothing to stop you from gutting the event code to populate the subroutine you wanted.

Not only that, but if the actions are the same for each of several text boxes, you can save yourself coding time, complexity, and headaches by unifying the code into a limited number of subroutines that generically handle your editing function just by pointing to the right control.
 

modest

Registered User.
Local time
Today, 17:05
Joined
Jan 4, 2005
Messages
1,220
I really wish sometimes we would solve the problem at hand and not create a workaround. I appreciate the help, but I know how to make a workaround. I'm working on database efficiency and clean code right now. But more importantly, I'm researching a way to call the button's click event dynamically.

Thanks guys, but I guess this will go unclosed like most of my problems =)
 

boblarson

Smeghead
Local time
Today, 14:05
Joined
Jan 12, 2001
Messages
32,059
Modest: I wouldn't call it a "workaround" because, as you should know, there are some things that Access can just plain not do as Microsoft has not extended full VB to the program. Given that we don't know for sure that it can't be done, as with many things, it's not necessarily a "workaround" but another means of accomplishing the same thing.

Now, it may be that this is possible to do and I wish you luck in finding the actual code that does this. If so, make sure to post it in the samples area. I'm sure that you'd have a whole lot of interest in that one.
 

Users who are viewing this thread

Top Bottom