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

Dynamically Call Procedure 1

Status
Not open for further replies.

dicktomatoes

Programmer
Sep 25, 2006
2
US
Greetings
I have a VBA module which has a number of procedures. I would like to create a function that would dynamically choose the procedure to run. I am querying the database and returning a result. That result is corresponds with the name of the procedure I want to run.

For instance, if the result of the query is "12345", I would want to Call procedure "12345". Next time if the reults are "54321" and I would want to call that procedure.
Any thoughts?
TIA!
 
Would a Select Case statment work

Select Case rs![Field].Text

Case "12345"
Call 12345()
Case "6789"
Call 6789()
Else
Call CatchElse()
End Select
 
also,

you could use eval():
Code:
Public Function test(func As String)
  On Error GoTo errHandler
  Eval (func)
  
exitHere:
  Exit Function
  
errHandler:
  Debug.Print "Error #" & Err.Number & ": " & Err.Description
  Resume exitHere
  
End Function

Public Function one()
    Debug.Print "This is function one"
End Function

Public Function two()
   Debug.Print "This is function two"
End Function

eg calling the function test with the parameter "one" will call function one, e.g
Code:
? test("one()")

Also, if your in a form or "class module" then you could use CallByName. See help file for more info. However, note that it is an 'unsafe' function, see here



cheers,
dan.
 
Thanks to you both for the very valuble suggestions. The eval worked great! My final syntax was on the eval was

Eval (FunctionToRun & "()")

It took a few minutes for me to realize I needed to include the parens with quotes when it kept telling me it couldn't find the name referenced.

Many thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top