Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scope of public variables/subs/functions from within form modules? 1

Status
Not open for further replies.

jgoodman00

Programmer
Jan 23, 2001
1,510
Can anyone clarify this, because everything I read states that any of the above declared with the keyword public become available to all procedures within all modules. I tried this, but it will not let me call a public funtion written in one form module from another form module.

I have checked the help file & a few books to try & clarify this, but they all state what I thought was the case, which is the above....

Any ideas?? James Goodman
j.goodman00@btinternet.com
 
If the Public Keyword is used in a module it is accessable by ALL.

If the Public Keyword is used in a Form it is only accessable to the rest of the Application when the Form is loaded, if the form is unloaded or closed it will NOT be accessable.

HTH

Psychpt@Hotmail.com
 
That is the way I thought it would be, but even with the form open (it opens as a sub-form anyway), it is still throwing the 'Sub or Function not defined' error. Has anyone tried this, because I have even tried declaring public variables, & they are not accessible outside that module, unless these entities are declared within a standard (i.e. non form/report) module....

James Goodman
j.goodman00@btinternet.com
 

You can define methods and properties of your own that the form exposes. You will be using the Property Let, Property Get and Public Subs and Functions. You can create user-defined properties. The process is similair to developing class modules. Search for User Defined Properties and Class Modules in the on line help and at the Microsoft Web-Sites
 
To call the variable from another form:
<Form_FormName >.<variable>
 
In other words, procedures, even public ones, in forms, belong first to the form. They are class modules, as opposed to freestanding ones. So just like the variable, you have to call <Form_FormName>.<ProcedureName> from elsewhere. And only, of course, if the form is loaded.
 
I thought this was correct, but to clarify I sent an email to the author of some of the microsoft press books. This is the reply he sent me:
James-

Access (95 and later) has two types of modules - Public modules and Class
modules.

You define all Public modules on the modules tab in the database window. If
you click on the new button while on the modules tab, Access creates a
Public module by default. Any Public function you declare in a public
module can be called from anywhere in Access, including from event
properties and queries. Any Public sub you declare in a public module can
be called from anywhere in code.

All Form and Report modules are Class modules. You can also define a Class
module in the database window by choosing Class Module from the Insert menu.
Class modules are Objects, and they become active only when an instance of
the class is active. Access instantiates form and report class modules
whenever the form or report is open. When you declare a Function or Sub as
Public in a class module, they become Methods of the Class. You can execute
them (as long as the object is active) just like you would execute the
method of any other object:

' Example of built-in method:
Set rst = db.OpenRecordset(&quot;MyQuery&quot;)

' Example of a custom method:
Form_MyForm.DoSomething

In the second case, Access looks for a public Sub called &quot;DoSomething&quot; in
the form named &quot;MyForm&quot;.

Note that you can also define custom Properties for form and report objects
by coding Property Let and Property Get and Property Set public functions
within the class module.

Even when a form is open as a subform in another form, you can still get to
its public functions and subs directly. Let's say you have a form called
&quot;MySubForm&quot; that has the following code in it:

Public Sub Hello()
MsgBox &quot;Hello&quot;
End Sub

When the parent form is open in form view, you can execute this method by
saying:

Form_MySubform.Hello

Hope that helps...
John Viescas, author
&quot;Running Microsoft Access 2000&quot;
&quot;SQL Queries for Mere Mortals&quot;

P.S. Feel free to post this back in the newsgroup where you originally asked
your question.



Hopefully this will clear things up for future people! James Goodman
j.goodman00@btinternet.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top