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

ID: Q140464

The information in this article applies to:

SUMMARY

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

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:

1. Create a new database called ScopeTest.

2. 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()

3. 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

4. Type the following function:

      Function Form1_SayHi()
         Form1Var = "Hi!"
         MsgBox Form1Var
      End Function

5. 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()

6. 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

7. Close the module. Close form Form2.

8. 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.

9. 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.

REFERENCES

For more information about scope, search the Help Index for "scoping levels," or ask the Microsoft Access 97 Office Assistant.

For more information about class modules, search the Help Index for "class modules," or ask the Microsoft Access 97 Office Assistant.

Additional query words:

Keywords          : kbprg MdlGnrl 
Version           : 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto

Last Reviewed: November 21, 1998