Dynamic Selection of a user-defined Function

P

Phesunov

Is it possible to dynamically call a function based information provided in
a table field? That information being the name of the function.

I'm looking to consolidate 30 different functions to be triggered from a
form based on the user's input on what calculation process to use. I could
give each function a number reference in a table to call a hard coded
function name based on that reference number but I was looking for a way to
simplify it by incorporating the function name in the table itself.

Below is a sample procedure and one function. [Function Call] is a text
field in a form containing the name of the function (i.e. FDM_7_User(A, B, C)
). I'm trying to establish FCall as a variable to hold the name and to call
the function from. Any ideas would be helpful.

Thank you
========================
Private Sub BTN_Calculate_Click()
Dim A As Variant
Dim B As Variant
Dim C As Variant
Dim Answer As Variant
Dim FCall As Object
A = 5
B = "Tbl_Alpha"
C = True
'[Function Call]="FDM_7_User(A, B, C)"
Set FCall = [Function Call]

'This works
Answer = FDM_7_User(A, B, C)
'This doesn't
Answer = FCall

MsgBox (Answer)
End Sub

-----------------------------
Function FDM_7_User(A, B, C) As Variant
'A = Qty
'B = Source Table Name
'C = Local Functional need
MsgBox ("FDM7")
FDM_7_User = "Done"
End Function
 
O

Ofer Cohen

I think whatyou are looking for is using Eval

for example
Dim FuncName as String, FuncRes
FuncName = "FunctionName()"
FuncRes = Eval(FuncName )
 
P

Phesunov

Yes Eval () is what I was looking for.
The one thing it dind't do was pass the (A,B,C) in the function as variables
so I changed the function call name in the table to be just () then used a
replace to provide the data from the varialbes.

Thank you!

Private Sub BTN_Calculate_Click()
Dim A As Integer
Dim B As String
Dim C As Integer
Dim FuncName As String, FuncRes
Dim QU As String
A = 5
B = Nz([Source Table], "")
C = 1
QU = Chr(34)
FuncName = Replace(Nz([Function Call], ""), "()", "(" & A & "," & QU & B
& QU & "," & C & ")")
FuncRes = Eval(FuncName)
MsgBox (Nz(FuncRes, "Error"))
End Sub


Ofer Cohen said:
I think whatyou are looking for is using Eval

for example
Dim FuncName as String, FuncRes
FuncName = "FunctionName()"
FuncRes = Eval(FuncName )

--
Good Luck
BS"D


Phesunov said:
Is it possible to dynamically call a function based information provided in
a table field? That information being the name of the function.

I'm looking to consolidate 30 different functions to be triggered from a
form based on the user's input on what calculation process to use. I could
give each function a number reference in a table to call a hard coded
function name based on that reference number but I was looking for a way to
simplify it by incorporating the function name in the table itself.

Below is a sample procedure and one function. [Function Call] is a text
field in a form containing the name of the function (i.e. FDM_7_User(A, B, C)
). I'm trying to establish FCall as a variable to hold the name and to call
the function from. Any ideas would be helpful.

Thank you
========================
Private Sub BTN_Calculate_Click()
Dim A As Variant
Dim B As Variant
Dim C As Variant
Dim Answer As Variant
Dim FCall As Object
A = 5
B = "Tbl_Alpha"
C = True
'[Function Call]="FDM_7_User(A, B, C)"
Set FCall = [Function Call]

'This works
Answer = FDM_7_User(A, B, C)
'This doesn't
Answer = FCall

MsgBox (Answer)
End Sub

-----------------------------
Function FDM_7_User(A, B, C) As Variant
'A = Qty
'B = Source Table Name
'C = Local Functional need
MsgBox ("FDM7")
FDM_7_User = "Done"
End Function
 

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