ACC2: Form and Report Modules Have Local ScopeID: Q112726
|
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.
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:
Form1
--------------------------
Command button: Button0
Caption: Form1_SayHi
OnClick: =Form1_SayHi()
Function Form1_SayHi()
MsgBox "Hi!"
End Function
Form2
--------------------------
Command button: Button0
Caption: Form1_SayHi
OnClick: =Form1_SayHi()
Function Run_Form1_SayHi()
Run_Form1_SayHi = Form1_SayHi()
End Function
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
Last Reviewed: April 3, 1999