reference a function name by the value in a string variable

M

Mike P

I wish to build the name of the function in a string, then reference it. But
I am unable to figure this out. In this example, there is a function I wish
to call name Fun1.

Dim str As String
str = “Fun1()â€
'How do I now call “Fun1()†by using the variable str

This may sound odd, but I will have about 50 functions with the same
name-base and number added, Fun1 – Fun50. I wish to populate a table with
the function names, pull the string from the table, then call the function in
VB code.

Any ideas on how to do this?

If I can't use Functions, can I use Procedures?

I am just trying to cut the code from 50 If Then statements down to a small
loop with an incremental counter.

Thanks in advance for your help!
 
J

John W. Vinson

I wish to build the name of the function in a string, then reference it. But
I am unable to figure this out. In this example, there is a function I wish
to call name Fun1.

Dim str As String
str = “Fun1()”
'How do I now call “Fun1()” by using the variable str

This may sound odd, but I will have about 50 functions with the same
name-base and number added, Fun1 – Fun50. I wish to populate a table with
the function names, pull the string from the table, then call the function in
VB code.

Any ideas on how to do this?

If I can't use Functions, can I use Procedures?

I am just trying to cut the code from 50 If Then statements down to a small
loop with an incremental counter.

Thanks in advance for your help!

Take a look at the Eval function:

strFunction = "Fun1()"
Eval(strFunction)

The need to do this suggests that a different design should be considered! How
the heck do these fifty functions differ? Is there some way to make this truly
table driven?
 
J

John Spencer

Take a look at the Eval function. It may work for you.

'====================================================
John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
'====================================================
 
M

Mike P

Thank you. After your suggestin, I was able to find this snipit on MS-Help:

The following example assumes that you have a series of 50 functions defined
as A1, A2, and so on. This example uses the Eval function to call each
function in the series.

Visual Basic for Applications
Sub CallSeries()

Dim intI As Integer

For intI = 1 To 50
Eval("A" & intI & "()")
Next intI

End Sub
 

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