Run functions dynamically

bubu678

New member
Local time
Today, 15:33
Joined
Oct 16, 2006
Messages
9
I have functions in a module that I would like to execute by "building" the function name.


example: In module A I have

Function AAA (ByVal Arg1, ... ByVal Argn)
...
End Function

Function BBB (ByVal Arg1, ... ByVal Argn)
...
End Function

Function CCC (ByVal Arg1, ... ByVal Argn)
...
End Function


I would like then to run the appropriate one something like this:

IF case1 THEN
sFunction = "AAA (arg1, ... argn)"
else
sFunction = "BBB (arg1, ... argn)"
END IF

EXECUTE sFunction

Can that be done in Access 2007?


Thanking everyone in advance...
 
Thanks for the quick reply.

I forgot about EVAL(). Will try it out now..
 
May I ask why though?

I shudder to even think about using Eval() and there's also CallByName() in scope which is more narrower but still fairly questionable. I do think that for most parts, any perceived for dynamic code execution can be actually satisfied with different approach that's not as risky.
 
May I ask why though?

I shudder to even think about using Eval() and there's also CallByName() in scope which is more narrower but still fairly questionable. I do think that for most parts, any perceived for dynamic code execution can be actually satisfied with different approach that's not as risky.

To add, IMO any program properly designed would not need a requirement like this.
 
To add, IMO any program properly designed would not need a requirement like this.

thats a bit harsh

I have an app that I use eval.

I want a standard function to be able to call a library function, which changes depending on some external setting. I may need to add new library functions, without having to edit the code for the function. so I have a table that stores the name of the function I want to run (which users can edit)

Then I can use (effectively)


sub myfunc

eval("stored function name")

end sub
 
It took me a while to figure out what IMO meant when I first saw it on this forum :)
 
I'm assuming that even the number of parameters can't be prediucted either - hence reason why Run wasn't considered?

Included for such dynamic execution as part of the Application OM for each Office app (shock horror - consistency! ;-p).
i.e. more targeted than Eval. Less specific than CallByName. But, inevitably, requires the correct number of parameters to be passed to it - as actual parameters (i.e. not concatenated into a string).

I have no particularly strong opinion one way or the other on the validity subject at hand though.

Cheers.
 
callbyname might have worked - not sure

but i got it working with eval, and that was good enough.

I couldn't see a way of being able to (effectively) pass a function name into a call any other way.

even if i used a case statement, i would have to modify it to allow for different calls. with eval, i just need to make sure the target sub exists.

I suppose what it is really doing is passing the address of the called function to a master function - and there didn't seem any way to do thisother than with eval.
 
In terms of processing, anything CallByName() can do Eval() can do it but in terms of the return value, Eval() is a bit limited - the return value must be String. With respect to where either of these functions can be used, Eval() is recognised by the JET engine in a SQL statement in Access, whilst CallByName() isn't recognised. CallByName() does have another limitation in that you must specify the calltype - let,get,set or method.

So yes as already mentioned, it all depends on what you want to do. I'm not against using either of them.
 

Users who are viewing this thread

Back
Top Bottom