Variables are stored momentarily in our computer’s memory. Therefore, it is significant for us to know when our variable goes out of scope or, in other words, when we can lose the value of our variable. Variables derive their scope from either their location in the program code or their declaration type.

Variables held in a procedure with the keyword Dim have what’s known as procedure-level scope; they are called local variables. That is, the variable keeps their value only throughout the implementation of that procedure.

We can also declare variables in a procedure with the keyword Static. Variables declared with the keyword Static retain their value the entire time your program is executing.

The following is an example of a variable called myString declared with the keyword Static and the variable myDate declared with the keyword Dim:

Static myString as String
Dim myDate as Date


Variables declared with the keyword Dim in a form’s code window, but outside of any sub procedures or functions, are considered form-level variables. These code areas outside procedures are identified as the General area. Form-level variables are accessible to any procedures or functions located in that form’s code window. We can use them when you want a variable to retain its value or scope when moving from one procedure to another.

You can also declare variables in standard code modules. Variables declared in a standard code module normally have one or two types of scope. If you declare a variable inside the standard code module with the keyword Dim or private, then it is available to only that standard code module. However, if you declare the variable with the keyword Public, then it is available to all modules and procedures in your application (and it is also known as a global variable).