In Unit Seven, we discussed variable declarations. You learned that variables can be global or local. More attention needs to be taken to these two terms.
Not only can code be private or public, but variables cna also have public and private scope, although programmers usually refer to them as global and local. The difference is based on how available the variables are from surrounding code.
A local variable can be used only by code to which it is visible (can be used by the procedure it is located in)
A global variable can be used by code outside of the procedure that it is located in.
The term scope refers to the availability of a variable form within the application's code.
You already have experience working with local variables. Local variables are declared within a procedure using the dim command. These local variables cannot be used by code outside of its procedure.
One solution is to make the variables public by declaring them in the declarations section of the form module. When a variable is declared in the declaratoins section, it is made public (global) and can be used by all the procedures in that form module.
However, this is dangerous. A rule of programming is that the programmer must avoid making variables public to all the procedures in a module. This rule exists to make your code easier to debug. Variables that must be used by more than one procedure must be designed that that they are only visible by those procedures that need to use that variable. The rest of the procedures in the module should not even know that the variable even exists.
How is this done? The answer is referred to as passing data and it is covered in the next lesson.