Connecting Tech Pros Worldwide Forums | Help | Site Map

CallByName to Module rather than to Form

Edlueze
Guest
 
Posts: n/a
#1: Nov 13 '05
I use the CallByName function quite regularly, but the sub routines
that I have always called have been located within the form code. The
call I use looks something like this:

Dim CallObjectForm As Form
Set CallObjectForm = Forms("My Form")
CallByName CallObjectForm, "My_Form_SubRoutineCall", VbMethod

I now want to call code that is located within a standard module and I
am not having any luck. I'm trying to use the following code:

Dim CallObjectModule As Module
Set CallObjectModule = Modules("My Module")
CallByName CallObjectModule, "My_Module_SubRoutineCall", VbMethod

Thanks for any ideas!


MGFoster
Guest
 
Posts: n/a
#2: Nov 13 '05

re: CallByName to Module rather than to Form


Edlueze wrote:[color=blue]
> I use the CallByName function quite regularly, but the sub routines
> that I have always called have been located within the form code. The
> call I use looks something like this:
>
> Dim CallObjectForm As Form
> Set CallObjectForm = Forms("My Form")
> CallByName CallObjectForm, "My_Form_SubRoutineCall", VbMethod
>
> I now want to call code that is located within a standard module and I
> am not having any luck. I'm trying to use the following code:
>
> Dim CallObjectModule As Module
> Set CallObjectModule = Modules("My Module")
> CallByName CallObjectModule, "My_Module_SubRoutineCall", VbMethod
>
> Thanks for any ideas!
>[/color]

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

The CallByName only works w/ class objects. A standard module is not
considered a class object. You can call a routine in a standard module
using the following syntax:

Function w/ return:

variable = ModuleName.FunctionName <(params)>

Sub:

ModuleName.SubName <params>

<> indicates optional.

--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQnaAMIechKqOuFEgEQIUPACglyBLh4vliGCrH/UAytT593WG6gMAnAwV
jN/QDY0Xd9kOe4BKpvcjbTkF
=yq5t
-----END PGP SIGNATURE-----
Edlueze
Guest
 
Posts: n/a
#3: Nov 13 '05

re: CallByName to Module rather than to Form


Thanks for the response. I use the CallByName routine to call a Sub
routine via a string that contains the name of the Sub routine. Is
there any way to call a Sub routine that is physically located in a
standard module (as opposed to a Form or Report) in this way (ie with a
string containing "Sub_Name")? Do the Form or Report class objects
somehow inherit the standard module routines?

Thanks!

MGFoster
Guest
 
Posts: n/a
#4: Nov 13 '05

re: CallByName to Module rather than to Form


Edlueze wrote:[color=blue]
> Thanks for the response. I use the CallByName routine to call a Sub
> routine via a string that contains the name of the Sub routine. Is
> there any way to call a Sub routine that is physically located in a
> standard module (as opposed to a Form or Report) in this way (ie with a
> string containing "Sub_Name")? Do the Form or Report class objects
> somehow inherit the standard module routines?[/color]

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

You can use the Eval() function:

strProcedure = "FunctionName()"
Eval(strProcedure)

The routine in the Eval() function has to be the name of a Function not
a Sub.

If you "know" the name of the procedure you want to run & where it is,
which it seems you do, why not use:

ModuleName.RoutineName

?? This was stated in my first response to you're original post.

E.g.:

intRound = Math.Round(dblPercentage,2)

Which calls the Round() function in the module Math and returns the
results into the intRound variable.

The Form/Reports do NOT inherit standard modules. There is no
inheritance in Access.
--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQnaTlYechKqOuFEgEQIWvgCg5oEpVAnJXqzchsWDaJtnN2 XvlSQAoLOd
mbEBCJEIA/8OcnUZ7f3Cwe5c
=fc1l
-----END PGP SIGNATURE-----
Edlueze
Guest
 
Posts: n/a
#5: Nov 13 '05

re: CallByName to Module rather than to Form


Great! Eval() works perfectly - I learn something new everyday.

Let me try and help you understand what I'm doing: 99% of the time I am
using a direct call to the procedure as you suggest. But the remaining
1% of the time I call the procedure via CallByName. In these situations
I have a pop-up that can be opened by any one of a number of forms, and
when that pop-up closes it needs to make a return call to the form that
opened it. I pass the name of the calling form and return sub routine
via two strings (actually more because I need to take into account
parent forms and grandparent forms). This allows me to use the same
pop-up to gather information in a standard way for lots of different
calling forms.

I ran into this particularly problem when I tried extending the basic
workflow to Controlbars. I put the code that manages my Controlbars
into standard modules rather than into the forms like everything else,
so the return call needed to be made to the standard module. This is
where I ran into trouble! The Eval() function now allows me to do this,
whereas the CallByName only allowed me to make the return call to a
form.

Steve Jorgensen
Guest
 
Posts: n/a
#6: Nov 13 '05

re: CallByName to Module rather than to Form


On 2 May 2005 20:59:31 -0700, "Edlueze" <edlueze@onegen.com> wrote:
[color=blue]
>Great! Eval() works perfectly - I learn something new everyday.
>
>Let me try and help you understand what I'm doing: 99% of the time I am
>using a direct call to the procedure as you suggest. But the remaining
>1% of the time I call the procedure via CallByName. In these situations
>I have a pop-up that can be opened by any one of a number of forms, and
>when that pop-up closes it needs to make a return call to the form that
>opened it. I pass the name of the calling form and return sub routine
>via two strings (actually more because I need to take into account
>parent forms and grandparent forms). This allows me to use the same
>pop-up to gather information in a standard way for lots of different
>calling forms.
>
>I ran into this particularly problem when I tried extending the basic
>workflow to Controlbars. I put the code that manages my Controlbars
>into standard modules rather than into the forms like everything else,
>so the return call needed to be made to the standard module. This is
>where I ran into trouble! The Eval() function now allows me to do this,
>whereas the CallByName only allowed me to make the return call to a
>form.[/color]

Just one more note...

The Eval solution works in Access, and there are similar functions in VBA for
other Office products, but there's no similar function in VB. If you ever
find you want to use CallByName in VB, just arrange for the procedure to be
called to be in a class instance. You can keep an instance of the class in a
global variable contained in a standard module.

Closed Thread


Similar Microsoft Access / VBA bytes