Execute Procedure using Variable name

In MS Access is there a way to call procedure where the procedure name is referenced inside a variable name?

Example:  
 
Dim MyProcedureName as String
       MyProcedureName = "TheProcedure"

Call  MyProcedureName
leeroypitreAsked:
Who is Participating?
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
Try this:

Dim MyProcedureName as String
       MyProcedureName = "TheProcedure"

EVAL(MyProcedureName)

mx
0
 
Boyd (HiTechCoach) Trimmell, Microsoft Access MVPCommented:
You must use a macro.  

This is one of the few instances where a Macro can do something that can;t be done in VBA.
0
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
Specifically:


Public Function aaaaQuickTest() As Boolean

    Dim Fx
    Fx = "aaaTest5()"
    Eval (Fx)
   
End Function


Public Function aaaTest5()

    MsgBox "aaaTest5"
 
End Function

In the Immediate Window:

?aaaaQuickTest()
results in the Message Box being displayed.

mx
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Boyd (HiTechCoach) Trimmell, Microsoft Access MVPCommented:
The Procedure has to be a function that you call using the Macro command RunCode

0
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
Back to your example:


Dim MyProcedureName as String
       MyProcedureName = "TheProcedure()"  ' add parens

EVAL(MyProcedureName)

and "TheProcedure()" needs to be a Function, not a Sub.

mx
0
 
Boyd (HiTechCoach) Trimmell, Microsoft Access MVPCommented:
mx, I forget about Eval. I started using macro for this back with Access 2.
0
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
Yeah ... no need for Macros now ... Eval() does it ....

mx
0
 
NorieVBA ExpertCommented:
If it's a sub can't you use Application.Run?
0
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
Yes ... that is correct ... like so

Public Function aaaaQuickTest() As Boolean

    Dim Fx
    Fx = "aaaTest5"   ' no parens this time
    'Eval (Fx)
    Application.Run Fx
End Function

Public Sub aaaTest5()

    MsgBox "aaaTest5"
 
End Sub

Forgot about the the Run Method, even though I've used it before.  Good for running code in an external db.

mx
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.