VB 6 Variable Scope


The scope of a VB 6 variable decides the lifetime of that variable. There are 3 levels of scope for a variable in Visual Basic 6. In this article, you will learn about the scope of variables with examples in a Visual Basic 6 project.

Level of Scope

The scope in many programming languages is either a global scope or a local scope. We will list out the global scope and local scope for a variable in visual basic 6.

Visual Basic 6 has 3 levels of scope:

  1. Module Level with Public (Global)
  2. Form or Module level (Global only to the Module).
  3. Procedure Level (Local scope)

Module Level With Public

Variables declared in the general section of the code editor using keyword Public are visible in all forms and modules throughout the VB 6 Project.



In this example, we declared two variable – test and number – as integers in the (General) section of Module 1. These integers will be available throughout the project and you can manipulate as and when necessary.

Global Declaration using Public
Figure 1 – Global Declaration using Public

Form Or Module Level

In this level, the variable is declared under (General) section of Visual Basic code editor. The keywords – Dim, ReDim, Static and Private are used to declare the variables.


In the example below, the form name is  “Accounting” and we have declared two variable using Dim statement. The scope of these variables is limited to the form only.

Form Level Global Declarations
Figure 2 – Form Level Global Declarations using Dim Statement.


Each procedure in VB 6 has its own variables. Those variables are local to the procedure, and cannot be used elsewhere. The keyword – Public cannot be used for procedures.


Local Variable Declaration
Figure 3 – Local Variable Declaration

In the example above, the variable Number is local to the procedure Form_Load( ). This is because Form_Load ( )  procedure is by default a Private procedure. This form assigns a value of 1003 to variable Number when the form loads.


Visual Basic 6 For Dummies 1st Edition

Create your own custom Visual Basic programs fast for Windows 95, Windows 98, and Windows NT!

If you want to develop business applications for Windows, then Microsoft’s Visual Basic 6 programming environment is for you. With its powerful and friendly object-oriented interface, Visual Basic 6 is the ideal tool for creating easy-to-use and attractive interfaces of your own. Whether you want to build original programs from the ground up or take advantage of existing wizards, Visual Basic 6 For Dummies will help you design and debug your work – and accomplish all your goals!

  • Covers using Visual Basic 6 with Windows 95, Windows 98, and Windows NT
  • Walks you through the ins and outs of object-oriented programming in Visual Basic and using the Visual Basic toolset
  • Shows you how to make use of menus and submenus, write subprograms that everyone can share, save and retrieve information in databases, integrate ActiveX controls into your applications, and build attractive user interfaces with buttons, boxes, and scroll bars

From the fundamentals of writing BASIC code to the thrill of distributing your very own custom applications, Visual Basic 6 For Dummies brings a fresh perspective to programming in Visual Basic 6 that everyone can understand.