VB 6 Variable Declarations

If you have added the forms and controls to the project, the visual basic language will make them work.  The visual basic 6 language is the code or instructions once an event like click happens.


In VB 6 language, you must learn the following things.

  1. Declare variables, constants, functions and subroutines.
  2. Use strings, operators, conditions, loops and handle math.
  3. Understand how VB 6 format financial data and dates.

In this article, you will learn to declare variables and constants. We will learn rest of topics in future articles.

Variable Declaration in VB6

The coding part in VB6 starts with a variable declaration. A variable in vb6 is a name that hold a value. Visual basic 6 is a typed language, similar to C/C++, Java that supports all kinds of data types.


Dim [WithEvents] variable name [([subscripts])] [As [New] type]

The keywords inside square bracket are optional. Let us discuss each of these keywords briefly.

WithEvents – tells that varname is object variable that respond to events by an active x object. varname is name of the variable.

Subscripts – use only when you are creating an array of variables or object variables.

New – use new keyword when you create new object. A new instance of the declared variable is created.

type – the type refers to data type of the variable, it is not a keyword. VB6 supports following data types.

String (variable length)
String * [length]

The default type is variant which takes value of any data type.

For example,

Dim Car As String
Dim Price As Integer

Multiple Variable Declaration


You can declare more than one variables at a time using the Dim statement.


Dim [WithEvents] variable1 name [([subscripts])] [As [New] type], [WithEvents] variable2 name [([subscripts])] [As [New] type], ...

Example #1

To understand the relationship between variables and controls consider the following example.

Step 1: Create a form and a button control similar to the figure below.

Figure 1 - Create Form and a Command button
Figure 1 – Create Form and a Command button

Step 2: Right click on the command button and select view code which will open visual basic code editor for that button control. Add the codes as given in the figure below.

Figure 2 - Code for Command button
Figure 2 – Code for Command button

Note that the red circle marks the control name and green circle indicates the event that triggers this code in the figure above.

The result of the code is given below when the command button is clicked.

Figure 3 - Result when button is clicked
Figure 3 – Result when button is clicked

Option Explicit

Sometimes we misspelled or use a variable without declaring it. Without declaration its type is variant, but misspenlt variable can cause problems.

In general section of code window at form or module level use Option Explicit statement forcing variable declaration.

Constant Declaration

Constants are values that we wish to keep fixed throughout the program. They are hard to track in a program, therefore VB6 allows you to declare all constants with a name and an optional type. The Const keyword is used to declare constants.


[Public | Private] Const constant_name [As type] = expression

Example #2

Private Sub Command1_Click()

Const side = 2.5
Dim Area
Area = side * side
MsgBox ("Area = " + Str(Area))

End Sub

When the button is clicked it will output area, but the constant value side will not be altered.


Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.