Call procedure on a different form

Bee

Registered User.
Local time
Today, 08:15
Joined
Aug 1, 2006
Messages
487
Hi,

Is it possible to call a procedure that exists in one form on another form please?

Regards,
B
 
As long as it is a public procedure, and the ME.xxx references are not used.
BUT you are better off putting the procedure in a module then calling it from both forms.
 
Implied but not stated explicitly in FoFa's answer is that the other form must be open at the time.
 
The_Doc_Man said:
Implied but not stated explicitly in FoFa's answer is that the other form must be open at the time.
Thanks, forgot that little tidbit.
 
Yes you can have a mixture of functions and sub procedures in the same module. What you can't have is duplicate procedure names.
 
FoFa said:
Thanks, forgot that little tidbit.
I used DoCmd.OpenModule(), to open up the module and call my procedure, but it keeps returning an error.

code:
Code:
docmd.OpenModule("showHide", "ShowControl")
 
Last edited:
Sorry Bee, maybe I am confused about what you are trying to do. Why would you want to open the Module. What you need to do is either run a function or call a sub procedure.

To run a function just type the function name into code on your form or if trying to get a value for a text box then the control source for the text box should be:

=functionname()

To call a sub procedure in a form use:

Call ProcedureName.

There is no reason to refer to the module name that the procedures/functions are in.

Hope this helps.
 
You don't do that.

Instead, use this:

Code:
Call YourProcerhureName

If it's a function that requires some parameters, then

Code:
Call YourFunctionName(Argument1, Argument2....)

Of course, they have to be public.

HTH.
 
Banana said:
You don't do that.

Instead, use this:

Code:
Call YourProcerhureName

If it's a function that requires some parameters, then

Code:
Call YourFunctionName(Argument1, Argument2....)

Of course, they have to be public.

HTH.
When I call it my procedure using 'Call myprocedure', it returned this error.
'Object required'. When I debugged, it highlighted my listbox name that is on the form calling the procedure.

I am missing something here?
 
Fear Naught said:
Sorry Bee, maybe I am confused about what you are trying to do. Why would you want to open the Module. What you need to do is either run a function or call a sub procedure.

To run a function just type the function name into code on your form or if trying to get a value for a text box then the control source for the text box should be:

=functionname()

To call a sub procedure in a form use:

Call ProcedureName.

There is no reason to refer to the module name that the procedures/functions are in.

Hope this helps.
The reason I am wanting to call a procedure from a module to a form is because I want to call this procedure in many forms.
 
Is this an EVENT procedure? If so, you cannot do this. Event procedures MUST be local to the class module for the form.

You can make an event routine call a global routine but the event must first be fielded by a local class routine.
 
I think what Doc Man is trying to say, is there are some things "tied" in the event procedure to the form (so to say). Typically what you need to do when you setup a global procedure, is pass whatever it needs to work with from the event procedure in the form. Have it do it's work, and depending on what is required, return a value/s. So lets say you global module you want to call from many different forms is suppose to take a yes/no value from a checkbox, and update a record with current date (if yes), flag it as past due if No.

Your event procedure would "capture" the value of the check box and them pass this along with a primary key (it needs to know WHAT to update) to the procedure:

CALL MyProcedure(Me.MyCheckbox, Me.primaryKey)

SUB MyProcedure(CBvalue as binary, PK as long)
' Do work here
END SUB

SO what is happening is the main work is common and the same no matter what calls the global procedure. But each form isolates itself from the module.
That is rather simplistic explaination, but should give you an idea of what we are talking about.
Clear as MUD?
 
FoFa said:
I think what Doc Man is trying to say, is there are some things "tied" in the event procedure to the form (so to say). Typically what you need to do when you setup a global procedure, is pass whatever it needs to work with from the event procedure in the form. Have it do it's work, and depending on what is required, return a value/s. So lets say you global module you want to call from many different forms is suppose to take a yes/no value from a checkbox, and update a record with current date (if yes), flag it as past due if No.

Your event procedure would "capture" the value of the check box and them pass this along with a primary key (it needs to know WHAT to update) to the procedure:

CALL MyProcedure(Me.MyCheckbox, Me.primaryKey)

SUB MyProcedure(CBvalue as binary, PK as long)
' Do work here
END SUB

SO what is happening is the main work is common and the same no matter what calls the global procedure. But each form isolates itself from the module.
That is rather simplistic explaination, but should give you an idea of what we are talking about.
Clear as MUD?
Thanks for your help guys.
 

Users who are viewing this thread

Back
Top Bottom