Call Procedure via variable/field

  • Thread starter Thread starter Christopher Caswell
  • Start date Start date
C

Christopher Caswell

How can I execute a procedure that is named in a variable or a field in a
table? E.g.:

Procedure is called MyAfterInsert(). This procedure is named in variable
'X' or in a field. How can I get something to the tune of 'call X' to
execute MyAfterInsert()? How would I do this for a procedure with
arguments?
 
Christopher Caswell said:
How can I execute a procedure that is named in a variable or a field
in a table? E.g.:

Procedure is called MyAfterInsert(). This procedure is named in
variable 'X' or in a field. How can I get something to the tune of
'call X' to execute MyAfterInsert()? How would I do this for a
procedure with arguments?

If the procedure is a Function, rather than a Sub, then you can call it
by using the Eval function:

Dim X As String
Dim vResult As Variant

X = "MyAfterInsert()"
vResult = Eval(X)

Or, whether it's a Function or a Sub, so long as it's Public you can
call it using the Application.Run method:

Dim strProcName As String
Dim vArg1 As Variant
Dim vArg2 As Variant

strProcName = "MySub"
vArg1 = 123
vArg2 = "Second Argument"

Application.Run strProcName, vArg1, vArg2
 
You can do this with a function. (so, if you have some existing subs, you
can just add some functions to call those subs).


dim strFunc as string
dim strP1 as string

strFunc = InputBox("Run what function")
If strFunc = "" Then Exit Sub

strParm1 = InputBox("what value for parm 1")

Eval (strFunc & "(" & strParm1 & ")")

If the parm is going to be a string value, then you need to surround the
value with " (quotes)
 
Back
Top