The scope of a variable defines where in the program the variable is recognized. The lifetime of a variable describes how long it will persist.
If you declare your variable within a sub or function, the variable's scope is limited to that sub or function only. That's why you don't need to worry about variables conflicting with each other if you use the same name for a variable in another sub or function. The lifetime of that variable is the same as the sub or function—the variable lives only while the sub or function is running. As soon as the procedure ends, the variable is destroyed and the memory used by the variable is released. A subsequent call of the procedure creates the variable again and it has no memory of the previous existence.
At times you want your variable to exist outside of a particular sub or function. If you declare the variable in the General Declarations section of the module (located at the top of the module), your variable can have a longer scope and lifetime. You can declare the variable in two ways:
□ Use the Private keyword to make the variable available to any and all procedures within the current module.
□ Use the Public keyword to make the variable available anywhere in the entire application.
The following code sample illustrates how both the declaration and its location affect a variable's scope and lifetime:
Option Explicit 'Used to require variable declaration
Public txtCustomerName as String 'Scope is entire application Private txtVendor as String 'Scope is any procedure in this module Dim txtSupplier as String 'Scope is the current module
Private Sub GetCustomerName()
Dim txtCustomer as String 'Scope is limited to this sub End Sub
You might be wondering why the two statements that begin with Dim have different scopes. Use of the Dim keyword in the General Declarations section sets the scope of the variable to the module so it can be used by any procedure in that module. In the previous listing, txtVendor and txtSupplier are both module-level variables. They can be used anywhere within the module and anytime the module is loaded. txtCustomerName is a global variable. It can be used anywhere within any procedure in the application.
Use of the Static keyword enables you to create a local variable with an ongoing lifetime. There are several reasons why you might want to do this. If you needed to know how many times a particular procedure was run, you could simply declare a global variable and increment this variable every time the procedure runs. However, it's often easier to track the use of variables when they are declared within the procedure in which they're used. There's one big difference between using the Static keyword within the procedure and using the Public keyword in the General Declarations section to declare your variables. If you declare the variable with the Public keyword in the General Declarations section, you can use the variable anywhere within your application. If you use the Static keyword within a procedure, you can only use the variable within that procedure. The key thing is that the variable isn't destroyed when the procedure completes. The variable remains and retains its value for the next time that procedure is called. Keep in mind that the variable is still dedicated to the one procedure, so you cannot use the Static keyword to create a variable within Procedure A and use it within Procedure B.
Was this article helpful?