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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Referring to a Procedure By Reference

Status
Not open for further replies.

JeffCarlin

Programmer
Aug 16, 2001
33
0
0
US
Is there a way to reference a function by use of a variable. For example, function name is "MyFunction" and it takes parameter "A". If I set a variable (type variant??) named "myVar" to equal "MyFunction", will this work?

x = myVar("A")

I know it won't work the way I've shown it, but is there a way to make it work?

Thanks all.
 
Take a look at the CallByName function, here's a little example on how it works:

Private Sub Command1_Click()
MsgBox CallByName(Me, "ThisIsATest", VbMethod, "Hey it worked")
End Sub
Public Function ThisIsATest(OnlyATest As String) As String
ThisIsATest = OnlyATest
End Function

Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
 
Thanks Joe.

It's a little more complicated than your example, unfortunately. We won't be working off a form or data input of any kind; we'll be reading values from an ini file and calling various functions based on the value's, uh, value. So, I tried the CallByName function, using "Me" as param 1, but no go. So I tried "MyAppName" which also did not work. The remaining params are ok, but the 1st has me stumped. Here's what I've got:

x = CallByName(Me,"myFunction",vbMethod,MyVar)
This gives me a "Type Mismatch" on Param1.

Thanks.
 
Where are the Procedures declared?


Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
 
That's the problem, if you place them into a class module instead and do this it should work:

dim cMyClass as new NameOFYourClass
x = CallByName(cMyClass ,"myFunction",vbMethod,MyVar)

Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top