By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,779 Members | 1,291 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,779 IT Pros & Developers. It's quick & easy.

Using a string to call a function

P: n/a
I have a load of function names stored in a table which I want to use
to decide which function to run. I can select the function name but I
cannot work out how to call the function if I have the name of the
function as a string.
Normally in my code I would just call the function name but if I do
this with the string variable which is the function name I get an
error.
Can anyone help.
Alex
Nov 13 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 25 Dec 2004 08:32:04 -0800, al********@hotmail.com (Alex) wrote:

Check out the Exec function in the help file.
-Tom.

I have a load of function names stored in a table which I want to use
to decide which function to run. I can select the function name but I
cannot work out how to call the function if I have the name of the
function as a string.
Normally in my code I would just call the function name but if I do
this with the string variable which is the function name I get an
error.
Can anyone help.
Alex


Nov 13 '05 #2

P: n/a
Tom van Stiphout wrote:
On 25 Dec 2004 08:32:04 -0800, al********@hotmail.com (Alex) wrote:

Check out the Exec function in the help file.
-Tom.


Do you mean Eval()?

--
Bas Cost Budde, Holland
http://www.heuveltop.nl/BasCB/msac_index.html
I prefer human mail above automated so in my address
replace the queue with a tea
Nov 13 '05 #3

P: n/a
On Sat, 25 Dec 2004 17:52:41 +0100, Bas Cost Budde
<b.*********@heuvelqop.nl> wrote:

Oops - yes, I meant Eval.
Debug.Print Eval("SomeFunction()")
-> SomeValue

-Tom.
Tom van Stiphout wrote:
On 25 Dec 2004 08:32:04 -0800, al********@hotmail.com (Alex) wrote:

Check out the Exec function in the help file.
-Tom.


Do you mean Eval()?


Nov 13 '05 #4

P: n/a
I tried Eval and it does seem to run the function but the function displays
a form used as a progress counter and the form is not displayed.
In essence I have a continous form with about 50 records in it and the user
can select any number of these. For each selection there is a function to
run. My progress form shows which of the functions is currently running and
how far through the function it is. All of this is working except being able
to call the function using the function name stored as a string in a table.
Alex

"Tom van Stiphout" <no*************@cox.net> wrote in message
news:vf********************************@4ax.com...
On Sat, 25 Dec 2004 17:52:41 +0100, Bas Cost Budde
<b.*********@heuvelqop.nl> wrote:

Oops - yes, I meant Eval.
Debug.Print Eval("SomeFunction()")
-> SomeValue

-Tom.
Tom van Stiphout wrote:
On 25 Dec 2004 08:32:04 -0800, al********@hotmail.com (Alex) wrote:

Check out the Exec function in the help file.
-Tom.


Do you mean Eval()?

Nov 13 '05 #5

P: n/a
Paradigm wrote:
I tried Eval and it does seem to run the function but the function displays a form used as a progress counter and the form is not displayed.
In essence I have a continous form with about 50 records in it and the user can select any number of these. For each selection there is a function to run. My progress form shows which of the functions is currently running and how far through the function it is. All of this is working except being able to call the function using the function name stored as a string in a table. Alex


Perhaps the following brute force method is adequate:

Form code:

Dim varArray() As Variant

ReDim varArray(1)
varArray(0) = "My string"
varArray(1) = 3
MsgBox (EvaluateFunction("Function2", varArray()))
ReDim varArray(0)
varArray(0) = "New string"
MsgBox (EvaluateFunction("Function1", varArray()))

Module code:

Public Function EvaluateFunction(strFunctionName As String, varArray()
As Variant) As Variant
Select Case strFunctionName
Case "Function1": EvaluateFunction = Function1(varArray())
Case "Function2": EvaluateFunction = Function2(varArray())
Case Else
EvaluateFunction = strFunctionName & " is not in the function list."
End Select
End Function

Private Function Function1(varArray() As Variant) As String
Function1 = varArray(0)
End Function

Private Function Function2(varArray() As Variant) As Integer
Function2 = varArray(1)
End Function

Disclaimer: It's late and I'm tired. I didn't know if you needed
function arguments so I added varArray() for that purpose. Perhaps
ParamArray can be used to preserve argument names. Where's that
Functions collection Evaluate method hiding :-)?

James A. Fortune

I wonder if "works like a charm" only refers to charms that always
work.

Nov 13 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.