ACC2: Form and Report Modules Have Local Scope
ID: Q112726
|
The information in this article applies to:
SUMMARY
Moderate: Requires basic macro, coding, and interoperability skills.
This article describes the effect of "scope" of procedures and variables in
form and report modules. Scope refers to the extent to which an identifier
(that is, a constant, data type, variable, or routine) can be referenced
in a program.
MORE INFORMATION
The procedures and variables defined in a form or report module have
"local" scope; that is, they and their values do not exist outside of that
object. Attempting to call a function in a form or report module outside
that form or report results in a "reference to undefined function or array"
error message or other "unknown function name..." type of error. A call
made to a subroutine from outside the form or report results in a "syntax
error" message.
A reference to one of the form's variables from a different module produces
unexpected results. Due to the nature of modules and procedures, a new
variable of the same name is implicitly created with an Empty value
(Variant type 0). If Option Explicit has been specified in the module that
refers to the form's variable, then a "Variable not Defined" compile error
occurs.
When you plan to create a procedure or variable in a form or report module,
first determine that nothing outside of that form or report will require
that the procedure or variable exist. Any variable or procedure you
write in a form or report module should be useful to that form or report
only.
If you need to call a procedure from multiple forms or reports, place the
procedure in a global module. Do the same for any variables your code needs
to access from more than one form or report.
The following example demonstrates the scope of a function in a form module
and the errors that can occur:
- Create a new database.
- Create the following new form not based on any table and name it Form1:
Form1
--------------------------
Command button: Button0
Caption: Form1_SayHi
OnClick: =Form1_SayHi()
- In Form1's Design view, choose Code from the View menu. Form1's
module will be displayed. Type the following function in the module:
Function Form1_SayHi()
MsgBox "Hi!"
End Function
- Create the following new form not based on any table, and call it Form2:
Form2
--------------------------
Command button: Button0
Caption: Form1_SayHi
OnClick: =Form1_SayHi()
- Create a new module and type the following function in the module:
Function Run_Form1_SayHi()
Run_Form1_SayHi = Form1_SayHi()
End Function
- Open both forms. Click the command button on Form1. Note thata message
box with the text "Hi!" is displayed. This works because the function is
in scope in Form1.
- Click the command button on Form2. Note that an error message is
displayed. This is because the function is out of scope for Form2.
- In the module's Immediate window, type the following line, and then
press ENTER:
? Run_Form1_SayHi()
Note that the error message, "Reference to undefined Function or array,"
is displayed. The error occurs because the function is still out of
scope. Calling the function from anywhere except Form1 results in an
error.
NOTE: In Microsoft Access version 7.0, the scope of procedures and
variables has changed.
REFERENCES
Microsoft Access "Building Applications," version 2.0, pages 59-60
Additional query words:
cbf code behind forms
Keywords : kbusage FmsOthr
Version : 2.0
Platform : WINDOWS
Issue type : kbinfo