VB 6 Variable Declarations

Advertisements
Advertisements

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.

Syntax:

Advertisements
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.

Boolean
Byte
Integer
Long
Currency
Single
Double
Date
String (variable length)
String * [length]
Object
Variant

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.

Syntax:

Advertisements
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.

Syntax:

[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.

Visual Basic 6.0 Programming by Examples Kindle Edition

Visual Basic is one of the easiest to learn computer programming language.
Yes, it is obsolete, but all MS Office products include VBA (Visual Basic for Application) and if you learn VB, you will know VBA!
In my tutorial, I used VB 6 to explain step by step how to create a simple Visual Basic Application and a relatively complex one (a Patient Management system) that is using a database. A patient Management application source code is explained in detail.
You will learn how to design and create a database in MS Access and how to create tables and queries.
The book includes a sample application that shows how to use Windows API function.
You will learn how to convert VB program that can be run only in Visual Basic development environment to a distributable application that can be installed on any client computer.
For illustration, I included 128 screenshot images and links to a video.
You will be able to download from my website complete source code for 7 Visual Basic projects including a Password Keeper, a Patient Management and a Billing Management application.
Get Your Copy Today!

Advertisements
Advertisements
Advertisements