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

Call a specific function by using the value of a parameter

Status
Not open for further replies.

ThomasMatelart

Programmer
Apr 17, 2001
14
BE
Hi !

I want to call and execute a specific function. The name of the function to call is stored in a parameter of an other function.

How can I do it ?

Thanks !

ThomasM
 
Try looking into the CallByName function...Perhaps something like this.

Private Sub Command1_Click()
Dim var As Variant
CallByName Me, "myFunc", VbMethod, var
End Sub
Public Function myFunc(var As Variant)
MsgBox "hello"
End Function
 
Note that the called function or sub MUST be a method of an object. This is true in the above example, because myFunc is a method of the form (Me).

If you want to call form independent functions (or subs) by name, then they must be put into a class module, which eventually should be instantiated into an object.

e.g. Class Module clsUtilities contains:

Public Function myFunc()
MsgBox "hello"
End Function


The calling module (form or otherwise):

Dim myUtility As clsUtilities
Set myUtility = New clsUtilities

Dim strFuncName As String
strFuncName = "myFunc"
...
CallByName myUtility,strFuncName,vbMethod
...
_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top