Solved

Execute Procedure using Variable name

Posted on 2011-09-14
9
309 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 21
ID: 36538385
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 - Microsoft MVP, Access and Data Platform) earned 500 total points
ID: 36538401
Try this:

Dim MyProcedureName as String
       MyProcedureName = "TheProcedure"

EVAL(MyProcedureName)

mx
0
 
LVL 75
ID: 36538421
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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 21
ID: 36538439
The Procedure has to be a function that you call using the Macro command RunCode

0
 
LVL 75
ID: 36538450
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
ID: 36538471
mx, I forget about Eval. I started using macro for this back with Access 2.
0
 
LVL 75
ID: 36538494
Yeah ... no need for Macros now ... Eval() does it ....

mx
0
 
LVL 34

Expert Comment

by:Norie
ID: 36538682
If it's a sub can't you use Application.Run?
0
 
LVL 75
ID: 36538715
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

695 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