Method of calling a function where function name is stored in a variable or control

T

Tony Vrolyk

(This is a repost of a posting in microsoft.public.access which was an
accident, sorry)

Is there a way to call a function (or sub for that matter) where the
function/sub name is stored in a variable?

For instance lets say I have a table that stores some function names. On a
form I have a list box that displays the records in that table. For this
example lets say they all accomplish similar things and all require the same
numbef of arguments and none of them return a value.

If Call were the right method to accomplis this it might look sometihng like
this

Dim lngArgument as Long
lngArgument = 1234

Call Me.lstBox, lngArgument

In this case Call would run the function whose name is currently the
selected value in Me.lstbox.

Is such a thing possible? I am using Access 2002.

thanks
Tony Vrolyk
 
J

John Spencer

Take a look at the EVAL function, you can use it with functions

For instance

Eval("fGetOrdinal(234)")

So with your example, if the value in lstBox was the name of a public
function in a module this MIGHT work for you
EVAL(me.lstbox & "(" & lngArgument & ")")

--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..
 
T

Tony Vrolyk

You are beautiful person. I tried posting about this quite a while back and
no one who responde could quite help me. I did try the EVAL function but had
my syntax wrong. i was trying to do it without the parenthesis.

I just tested it and it works. that will make life so much easier.

thanks
Tony
 
J

John Spencer

Amen to that.

'====================================================
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
'====================================================
 
T

Tony Vrolyk

Now I am curious why you say that. If you meant to imply that my coding in
general must be bad if I fogot the parenthesis - I did not mean the ones
required by the EVal funtion but rather the ones required by the function
within the Eval.

i.e.
EVAL(me.lstbox & "(" & lngArgument & ")")

vs
EVAL(me.lstbox lngArgument)

Before the Eval I called them like this
fFunctionName Argument1

Having not used the Eval before I assumed that within the Eval you would
call a function the same way you would outside the Eval. In this case these
functions do not require the parnethesis when run on their own. So I don't
think that is an unreasonable assumption.

Tony
 
T

Tony Vrolyk

Now I am curious why you say that. If you meant to imply that my coding in
general must be bad if I fogot the parenthesis - I did not mean the ones
required by the EVal funtion but rather the ones required by the function
within the Eval.

i.e.
EVAL(me.lstbox & "(" & lngArgument & ")")

vs
EVAL(me.lstbox lngArgument)

Before the Eval I called them like this
fFunctionName Argument1

Having not used the Eval before I assumed that within the Eval you would
call a function the same way you would outside the Eval. In this case these
functions do not require the parnethesis when run on their own. So I don't
think that is an unreasonable assumption.

Tony
 
J

John Spencer

No, I wasn't being critical of your coding. What I meant was that debugging
code that has a problem is difficult enough without having to work out which
function was being called depending on which string was in the field could
be tough.

I have actually used a similar technique to handle some complex importation
of legacy data that had to be manipulated different ways depending on the
year of the data file and the field name of the data itself. Long Story -
and it was difficult to debug and I had written the code and was very
familar with what was supposed to happen.

--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..
 
T

Tony Vrolyk

Gotcha. Sorry to be so sensative.

I think in this case it will be relatively easy. I am keeping as much
similarity between the functions called by the Eval as possible - same
number of arguments, same argument types, etc..

Thanks again
tony
 

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