Solved

Execute Procedure using Variable name

Posted on 2011-09-14
9
298 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:leeroypitre
  • 5
  • 3
9 Comments
 
LVL 21
Comment Utility
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
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Access MVP) earned 500 total points
Comment Utility
Try this:

Dim MyProcedureName as String
       MyProcedureName = "TheProcedure"

EVAL(MyProcedureName)

mx
0
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
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
 
LVL 21
Comment Utility
The Procedure has to be a function that you call using the Macro command RunCode

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
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
 
LVL 21
Comment Utility
mx, I forget about Eval. I started using macro for this back with Access 2.
0
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
Yeah ... no need for Macros now ... Eval() does it ....

mx
0
 
LVL 33

Expert Comment

by:Norie
Comment Utility
If it's a sub can't you use Application.Run?
0
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

We were having a lot of "Heartbeat Alerts" in our SCOM environment, now "Heartbeat" in a SCOM environment for those of you who might not be familiar with SCOM is a packet of data sent from the agent to the management server on a regular basis, basic…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now