Microsoft KB Archive/140464

= ACC: Form and Report Modules Are Public by Default (95/97) =

Article ID: 140464

Article Last Modified on 1/19/2007

-

APPLIES TO


 * Microsoft Access 95 Standard Edition
 * Microsoft Access 97 Standard Edition

-



This article was previously published under Q140464



Moderate: Requires basic macro, coding, and interoperability skills.



SUMMARY
Microsoft Access has set the scope of procedures in form and report modules to be as consistent with object-oriented programming as possible. Variables in Microsoft Access class modules can be declared as Public in order to be available to other objects in a database. You can use a pre-declared identifier to refer to a form without the form having to be loaded into the Forms collection. This article shows you examples of how to use pre-declared identifiers to refer to objects in a class module.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.



MORE INFORMATION
The following example demonstrates the scope of a function and a variable in a form module:  Create a new database called ScopeTest.  Create the following new form not based on any table or query and name it Form1. Add the following command button to the form: Command button --     Name: Command0 Caption: Form1_SayHi OnClick: =Form1_SayHi  On the View menu, click Code to display the form's module. Type the following line in the module's Declarations section:

Public Form1Var as string

  Type the following function: Function Form1_SayHi Form1Var = "Hi!" MsgBox Form1Var End Function   Create the following new form, not based on any table or query, and call it Form2. Place the following command button on the form: Command button: Name: Command0 Caption: Form1_SayHi OnClick: [Event Procedure] Enter the following code in the event procedure for the OnClick property: [Form_Form1].Form1_SayHi Create another command button on the Form2 form: Name: Command1 Caption: Form2_ShowVar OnClick: =Form2_ShowVar   On the View menu, click Code to display the form's module. Type the following function in the module: Function Form2_ShowVar MsgBox Form_Form1.Form1Var End Function </li> Close the module. Close form Form2.</li> Open form Form1 in Form view. Click the command button on Form1. Note that a message box with the text "Hi!" appears. This works because the function is in scope in Form1. Close form Form1.</li> Open form Form2. Click the command buttons on Form2. Note that the same message box appears because the function and the variable are both still in scope for Form2. Note also that Form1 does not have to be open in order for these references to work.</li></ol>

<div class="references_section">