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

Last reviewed: August 28, 1997
Article ID: Q140464
The information in this article applies to:
  • Microsoft Access versions 7.0, 97

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 public, in order to be available to other objects in a database. You can refer to a form using a pre-declared identifier without the form having to be loaded into the Forms collection. This article shows examples of using 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.

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


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: August 28, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.