FAQ – Local and Global Variables

Description and exemplification of

  • local and global variables
  • A local variable is only used within a subroutine and can not be seen or accessed from other parts of the program.

    A Global variable can be passed to other parts of the program and can be seen and accessed from all parts of the program.

    The scope of a variable defines which part of the program can see the variable and change its value. Eg. the scope of a local variable is the subroutine it is in.

    From Scholar notes.

    1. If a variable is declared in the general declarations section with the Dim statement then it is Public or Global to the entire program.
    2. If it is declared as Public in the general declarations section then it is Global to the entire program.
    3. If it is declared Private in the general declarations section then it is Global to the entire form and modules.
    4. If declared within the sub procedure then the variable becomes Local to that sub procedure and will not be recognised elsewhere.