Microsoft KB Archive/210205

= ACC2000: Form and Report Modules Are Public by Default =

Article ID: 210205

Article Last Modified on 6/23/2005

-

APPLIES TO


 * Microsoft Access 2000 Standard Edition

-



This article was previously published under Q210205



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

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).



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 so as 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.



MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. The following example demonstrates the scope of a function and a variable in a form module:  Start Microsoft Access and create a new blank database called ScopeTest. Create the following new form not based on any table or query, and then name it Form1. Add the following command button to the form:

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  Close the module. Save and close Form1. Create a second new form, not based on any table or query, and call it Form2. Place the following command button on the form:

Name: Command0

Caption: Form1_SayHi

</li>  Type the following code in the event procedure for the button's OnClick property: [Form_Form1].Form1_SayHi </li> Create another command button on Form2:

Name: Command1

Caption: Form2_ShowVar

OnClick: =Form2_ShowVar

</li>  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. Save and close Form2.</li> Open 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 Form1.</li> Open 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 for these references to work.</li></ol>

<div class="references_section">