Calling a Sub From another Module

  • Thread starter Thread starter Patrick Graham
  • Start date Start date
P

Patrick Graham

I have a sub routine called lbxClientLkUp_AfterUpdate()
belonging to a list box in the parent form, and which I
have made public.

In a sub form I want to have the code in that sub routine
run when btnCancel is clicked.

How do I call the public subroutine in the parent form
from the subform?

It keeps saying the sub is not defined. :(
 
O.k. I've discribed this incorrectly.

The coding I had was all located in the parent and sub
forms code sections. Not in a module. (unless you guys
call these things modules as well)

I can move the listbox after update coding to an actual
module and call it from that with no problem.

However I would rather not have to move the code out from
the parent form, as this is slightly more confusing to
look at.

So can I leave the code in the parent form and call it
from the subform? if so how.
 
Patrick said:
I have a sub routine called lbxClientLkUp_AfterUpdate()
belonging to a list box in the parent form, and which I
have made public.

In a sub form I want to have the code in that sub routine
run when btnCancel is clicked.

How do I call the public subroutine in the parent form
from the subform?

It keeps saying the sub is not defined. :(


What did you try?

You can call a public sub in another open form with the
general syntax:
Forms!theotherform.procedurename
or using this shorter reference when calling from a subform
to the main form:
Parent.procedurename

This syntax is used because a form's module is a class
module (as opposed to a standard module) and its public
procedures are methods of the class.
 
The code behind a form is referred to as a module. In it are procedures for
controls, sub-routines, and functions. But this module is not generally
available to the rest of the database application. It is meant for the form
itself. A standard module (which you find in the Modules group of the main
database window) is a public module, meant for just what you want.

As Marshall points out, the form module is not a good place to store public
code; a standard module is the best place. If the form is not currently open
when you need to run the code there, it will not be available to other
forms. The code in a standard module is available regardless of which forms
are open.

So, tho it seems a bit more confusing, its the best approach and is much
less confusing than the kinds of complications and errors you're getting.
Just think of a standard module as your toolbox of sub-routines and
functions that are available to the whole application.
 
Move the sub into a separate module
Call it from both places

You will probably need to alter it as references to the local form
(Me.) will not work the same.

Sub CheckThings (frm as Form)
frm.TxtAltered = "Fred"
'Was me.txtAltered = "Fred"
end sub

Change lbxClientLkUp_AfterUpdate()
to
CheckThings Me


I forget the exact syntax fo refering to a forms parent, but its some
thing along the lines of me.parent.form so form the subform, run
CheckThings me.parent.form

Regards Greg Kraushaar
Wentworth Falls Australia
(Do not email - the reply address is a Spam spoofer)
(If you really must, remove all UCase and numbers)
 
Back
Top