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

Call (using variable instead of function name)

Status
Not open for further replies.

Wrathchild

Technical User
Aug 24, 2001
303
US
I'm trying to use Call with a variable that holds a function name to run it...is that possible? So instead of Call Function1 I would have Call x (where x = "Function1") I have a table of report names and the modules that need to be run when one is selected so I have the module name being returned and assigned to a variable but I can't figure out how to call it. I've tried the Callbyname function but I can't get the correct syntax; don't even know if that's what I need to use.
For Callbyname I've tried:
Callbyname Me, variablename, VbMethod
the variablename is coming thru ok, but I know the first part is wrong...need an object, but what? Form name didn't work.
 
Use the EVAL statement, for example:

dim strFunctionName as string

strFunctionName = "YourFunctionName"
Eval(strFunctionName)
 
thanks, but now I'm getting "Microsoft Access can't find the name 'Function1' you entered in the expression
 
The help files for 'Eval' show clear samples. I've never used Eval but I built a short function and used Eval successfully. For null functions you need to include the parenthesis and for parameters

My quick test:
?Eval("SayHi()")
Hi
Public Function SayHi()
Debug.Print "Hi"
End Function

Help Sample for using parameters:
Eval("StrComp(""Joe"",""joe"", 1)")

Steve King Growth follows a healthy professional curiosity
 
thanks, but not what I'm looking for...anyone else got an idea? I just want to replace Call Function1 with Call x, nothing more.
 
Your statement is confusing. 'Call' is reserved for a subroutine not a function. If you change your subroutine to a function you could then use Eval. And assuming x is your variable you would assign x with the name of the function.

x = "YourFunctionName()"
Eval(x)

Steve King

Growth follows a healthy professional curiosity
 
On click of button to run a selected report from a list box I have:

Private Sub cmd_run_Click()
Dim ctlSource As Control
Dim x As Variant
Set ctlSource = Me.lst_reports
x = ctlSource.Column(2) 'column 2 = Function name
Eval (x)

depending on which report is selected in the list box the function will be returned and run:
example:
Function Closures_category()
Me.Visible = False
DoCmd.Minimize
DoCmd.OpenForm "frmDateRange"
End Function

I selected the Closures by Category report so the Closures_category function was returned from column 2 and assigned to x, now I want to say Eval (x) but keep getting error messages saying it can't find "Closures_category". I've tried it with "Closures_category" & "Closures_category()" in the table but neither worked. this is driving me nutty!
 
In the immediate window paste the following code and execute it. Try any combination you want.

?Eval("Closures_category()")

Other things that may cause your problem are:
1) The function is not in a module but is in a Class (either code behind a form or a class). If in the same form that's fine. Scope from a form is (All public and private procedures in the same form and all public procedures from another module).
2) It could be that you are using a variant rather than a string on x. Try 'Dim x As String'.

Steve King
Growth follows a healthy professional curiosity
 
FYI,

If Closures_category is in a Class then you can't invoke it from the immediate window with the above line of code. You might then try pasting the code into a module and make sure it is public.

Steve King Growth follows a healthy professional curiosity
 
#1 was the problem!! Function was on the Form instead of in a separate module...thank you!!
 
Place the Function Closures_category() in a module (not in the form) and take out the me.Visible and docmd. Your example works for me.

Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top