Share this Page URL
Help

Chapter 5. Using Visual Basic to Automat... > Understanding Scope and Variables - Pg. 156

Using Visual Basic to Automate Your Database Scope and Lifetime of Variables 156 Like procedures, variables and constants have scope, and variables also have a lifetime. A variable declared within a procedure can be used only within the procedure in which it's declared. You can give a variable scope within a module by declaring the variable as public or private in the Declara- tions section of the module. If a variable is public within a standard module, any code in your data- base can use the variable. The scope of a variable depends on how and where the variable is declared, as follows: · If a variable is declared within a procedure, it is available only within that procedure. A variable declared within a procedure is known as a local variable. · A variable declared Private (or declared without a keyword, which is the same as Private ) in the Declarations section of a module is available only within that module. · A variable declared Public in the Declarations section of a module is available from any module within a project. A variable's lifetime indicates how long that variable exists in code. A variable can exist the entire time an application is running or only during the execution of a particular procedure. Table 5-2 summarizes scope and lifetime for procedures and variables. Table 5-2. Scope and Lifetime for Procedures and Variables Declaration Key- words Public Sub Public Function Public Sub Public Function Declaration Level Module level Module level Procedure level Procedure level Variable Scope and Lifetime Available to all modules in all projects Available to all modules in all projects N/A N/A