A.D.Tejpal
adtp at airtelmail.in
Tue Jun 23 13:18:12 CDT 2009
Arthur,
A function can be called by applying Eval() function upon a string variable holding the name of the function along with its arguments - if any.
A procedure can be run using Application.Run command. Here, the first argument is just the name of procedure, followed by optional other arguments representing arguments meant for the procedure.
A procedure disguised as function (no return value) can be run by using Eval() function.
Sample test routine given below, demonstrates the syntax applicable in the three different cases outlined above:
'============================
Sub Test()
Dim strCall As String
' Call a function with return value
' using Eval() function.
strCall = "Fn_Test_01('T')"
Debug.Print Eval(strCall)
' Run a procedure
' (using Application.Run command)
strCall = "P_Test_02"
Application.Run strCall, "ABC"
' Note - Trying to invoke above procedure via
' Eval() function will attract error
' (Function not found)
' Call a procedure disguised as function (No return value)
' using Eval() function.
strCall = "Fn_Test_03('KLM')"
Debug.Print Eval(strCall) ' Prints Null in immediate window
End Sub
'============================
Sample functions / procedures called in the test routine above, are also given below.
Best wishes,
A.D. Tejpal
------------
' Sample functions & procedures
'=====================================
Function Fn_Test_01(InputValue As String) As Boolean
MsgBox "This is Fn_Test_01"
Fn_Test_01 = IIf(InputValue = "T", True, False)
End Function
'--------------------------------------------------------
Sub P_Test_02(InputValue As String)
MsgBox "This is P_Test_02" & _
vbCrLf & "Input = " & InputValue
End Sub
'--------------------------------------------------------
Function Fn_Test_03(InputValue As String)
MsgBox "This is Fn_Test_03" & _
vbCrLf & "Input = " & InputValue
End Function
'======================================
----- Original Message -----
From: Arthur Fuller
To: Access Developers discussion and problem solving
Sent: Tuesday, June 23, 2009 20:54
Subject: [AccessD] Pseudo Pointer to a Function
Is there a way to pass the name of a function or procedure to another one?
The need for this might occur when the given procedure must branch in
various ways, and call a proc/function whose name depends upon the
code-path, as it were. Yes, one could write a CASE statement to deal with
this, and that is exactly what my current code does. But it set me to wonder
whether it's possible to pass the name of the function/procedure to call
rather than code the CASE statement.
A.