Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling Procedure Sub from a variable

Status
Not open for further replies.

sxschech

Technical User
Jul 11, 2002
1,033
US
I tried some of the examples found in posts here and perhaps I'm not understaning or am missing something as none of the suggestions have worked for me.
I have a combo box (which is replacing numerous buttons on the form) and the user selects the item then the code will call the sub that previously was activated by the button.

What I was hoping would happen is the following: User clicks on combobox, chooses tglEdit, code would then do a Call tglEdit_Click

I know I can use if or case statements, but thought that since each one is merely going to call an existing procedure I hoped it could have been done with one or two lines

Below are the different combinations I tried and each one gave an error such as can't find the procedure. (Please don't say "I see you have two stCall" as there will only be on, it's just I was commenting out one or the other, etc to see if that made a difference.)


Code:
Private Sub cboCombo_Click()
'Replaces buttons for editing, adding records and opening forms
'20140424 ss
    Dim stCall As String
    'stCall= Me.cboCombo
    'stCall = Me.cboCombo.Text '& "_Click()"
    'CallByName Me, stCall, VbMethod
    'Application.Run stCall & "_Click()"
    'Application.Eval stCall
    'Call stCall
    
End Sub

 
What about this ?
CallByName Me, Me!cboCombo & "_Click", VbMethod

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi PHV,

I tried your suggestion and got error 2465 Application-defined or object-defined error.
 
What are the values in your combo box? I realize they are supposed to be names of controls, but are they the names AND the procedures?

So instead of cmdMyCommandBox1, you should have cmdMyCommandBox1[highlight #FCE94F]_Click[/highlight]



"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
kjv,

Combobox is called cboCombo
Values come from a table and are the literal names of the existing subs that currently are attached to button click events

This line of code works when hard coded in the sub "Call tblEdit_Click"

A value in the table is "tblEdit"

When user clicks on the combo and chooses tblEdit, that value shows up in me.cboCombo

In the error box or debug window I can see the statement

Call me.cboCombo as "Call tblEdit_Click", but it doesn't perform the request. Not sure if what you are saying is to put the _Click into the value in the table that the combo box refers to?
 
And the existing subs are located in the same form as cboCombo ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi Guys,
Finally got it working. I changed the subs being called from Private to Public and now is working. Not sure why a hard coded call is ok with Private, but a parametized one needs Public even though all are contained in the same Form.

Code:
Private Sub cboCombo_Click()
'Replaces buttons for editing, adding records and opening forms
'20140424 ss
    Dim stCall As String
    On Error Resume Next
    stCall = Me.cboCombo 
    CallByName Me, stCall & "_Click", VbMethod
    'Call tglEdit_Click 'Hard Code version
End Sub
 
>why a hard coded call is ok with Private, but a parametized one needs Public even though all are contained in the same Form

All calls accessed by CallByName need to be Public.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top