A.D.Tejpal
adtp at airtelmail.in
Wed Jul 1 13:45:18 CDT 2009
Shamil,
Thanks for confirming. So nice of you.
William's post of 23-Jun-2009 gave the impression that he had been successful in using CallByName() function even for calling subs / functions located in standard / general modules. Apparently, that is not the case and there has been no confirmatory corroboration from him.
With your clarification, the thread attains proper completion.
In conclusion, for calling a sub or function using a string variable holding the name of the sub or function, the following considerations appear to apply:
1 - CallByName() function is meant (and is the preferred method) for calling public subs / functions belonging to form or class modules. (Subs / functions located in standard / general modules can not be called by this method). Typical syntax:
(a) Called proc is in the same form:
CallByName Me, "<<CalledProcName>>", VbMethod
(b) Called proc is in another form:
CallByName Forms("<<TargetFormName>>"), _
"<<CalledProcName>>", VbMethod
(c) Called proc is in a class:
CallByName PointerToClassObject, _
"<<CalledProcName>>", VbMethod
2 - For calling public subs / functions located in standard / general modules, Application.Run command or Eval() function can be used.
Note: For all the methods mentioned in 1 & 2 above, the called entity has to be public.
3 - If Eval() function is used, the called entity has to be a function (with or without return value). Otherwise error. Typical syntax: Eval(strCall) - where variable strCall holds the complete string depicting name of function as well as its arguments, e.g.:
strCall = "MyFunction('ABC')" ' Here, 'ABC' is the argument.
Debug.Print Eval(strCall)
4 - Application.Run command can handle subs as well as functions (if return value is not material). This command is not suited for calling subs / functions in form modules. Here, the first argument is just the name of procedure, followed by optional other arguments representing arguments meant for the procedure or function.
5 - As an academic interest, Eval() function can be used to call a public function in form modules also, if no argument is required to be passed. For example
strCall = "Forms('<<TargetFormName>>').MyFunction"
Debug.Print Eval(strCall)
Note: The function parenthesis are not to be used, i.e. not MyFunction(). Moreover, the target function gets activated twice. Therefore better use CallByName() function.
Best wishes,
A.D. Tejpal
------------
----- Original Message -----
From: Shamil Salakhetdinov
To: 'Access Developers discussion and problem solving'
Sent: Wednesday, July 01, 2009 20:15
Subject: Re: [AccessD] Pseudo Pointer to a Function
Hi A.D.,
I suppose CallByName will not work for standard/general modules'
subs/functions as CallByName is based (I guess) on COM IDispatch interface's
Invoke(...) method (http://en.wikipedia.org/wiki/IDispatch) and IDispatch
interface is implemented in MS Access/VBA only for custom classes' modules,
which are instantiated internally as COM objects.
--
Shamil
-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of A.D.Tejpal
Sent: Wednesday, July 01, 2009 5:55 PM
To: Access Developers discussion and problem solving
Subject: Re: [AccessD] Pseudo Pointer to a Function
This refers to my post of 27-Jun-2009, placed below. While response from
William is awaited, it would be nice if other members of this forum could
also kindly conduct a test and confirm whether any success with using
CallByName() function where the called procedure / function is in a general
module.
Best wishes,
A.D. Tejpal
------------