Procedure name as variable

B

Brian

Is there a way to recall a procedure name from a variable?

I am developing an app that has a table of clients, with different code to
be run, depending on the client. I loop through the Client table, and
depending on the client, run a different procedure because the things that
need to be done are very different for each client.

Rather than calling the procedure from within a Select Case on
Client.ClientID, I would like to have a field in the Client table called
ProcedureName. Then, when I run my code, I would like to look up the value of
this field for the specific client and call that procedure.

Although it was already obvious it would not work this way, I tried it and
got the expected Compile error:

Private Sub Test
Dim ProcName As String
ProcName = "DoThis"
Call ProcName 'got the error here
End Sub

Private Sub DoThis
DoThingsHere
End Sub
 
D

DaveT

Might try using Eval. I have a publiv function called RTest. RTest is a
function that requires two arguments (both long int). The first is string len
and the second is a seed number so that function returns a string (useful for
passwords, etc).

Anyway, here is code that will build string and call function. In your case,
you might build string to call using field values, or whatever.

Dim x As String
Dim p As String
Dim z
p = "RTest" 'function name
x = p & "(4,100)" 'equivalant to calling Rtest(4,100)
z = Eval(x)
Debug.Print z 'prints result
 
S

Stuart McCall

Brian said:
Is there a way to recall a procedure name from a variable?

I am developing an app that has a table of clients, with different code to
be run, depending on the client. I loop through the Client table, and
depending on the client, run a different procedure because the things that
need to be done are very different for each client.

Rather than calling the procedure from within a Select Case on
Client.ClientID, I would like to have a field in the Client table called
ProcedureName. Then, when I run my code, I would like to look up the value
of
this field for the specific client and call that procedure.

Although it was already obvious it would not work this way, I tried it and
got the expected Compile error:

Private Sub Test
Dim ProcName As String
ProcName = "DoThis"
Call ProcName 'got the error here
End Sub

Private Sub DoThis
DoThingsHere
End Sub

You can use the Eval function for this:

dummy = Eval("DoThis()")

Note that for this to work correctly, the procedure must be a function
(something that can be evaluated) rather than a Sub.
 
A

Albert D. Kallal

Stuart McCall said:
You can use the Eval function for this:

dummy = Eval("DoThis()")

Note that for this to work correctly, the procedure must be a function
(something that can be evaluated) rather than a Sub.

Correct, but you can use application.run to exectue a sub.

eg:

dim strSub as string

strSub = "test8"

Application.Run strSub
 
S

Stuart McCall

Albert D. Kallal said:
Correct, but you can use application.run to exectue a sub.

eg:

dim strSub as string

strSub = "test8"

Application.Run strSub

I always forget to mention this. I tend to make all my public procedures
functions, in case I later decide I want to return eg True for success. Or
call it from a comandbar or a control property etc.
 
A

Albert D. Kallal

Stuart McCall said:
I always forget to mention this. I tend to make all my public procedures
functions, in case I later decide I want to return eg True for success. Or
call it from a comandbar or a control property etc.


Yes, much the preferred approach, especially your comments about running
direct from custom menu bars..

So, while I mention the "run" method, I very much on board with using
functions when possible....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top