ACC2000: Form and Report Modules Are Public by Default (210205)



The information in this article applies to:

  • Microsoft Access 2000

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:
  1. Start Microsoft Access and create a new blank database called ScopeTest.
  2. 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()

  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. Close the module. Save and close Form1.
  6. 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

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

    Name: Command1
    Caption: Form2_ShowVar
    OnClick: =Form2_ShowVar()

  9. 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
    					
  10. Close the module. Save and close Form2.
  11. 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.
  12. 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.

REFERENCES

For more information about scope, click Microsoft Visual Basic Help on the Help menu, type understanding scope and visibility in the Office Assistant or the Answer Wizard, and then click Search to view the topics returned.

For more information about class modules, click Microsoft Visual Basic Help on the Help menu, type module and class module commands (insert menu) in the Office Assistant or the Answer Wizard, and then click Search to view the topics returned.

Modification Type:MajorLast Reviewed:6/23/2005
Keywords:kbhowto kbinfo kbProgramming KB210205