Beginners Guide to Visual Basic 6 – Part 1

I’ve seen couple of posts in the site where people have like asked the basic questions about VB. For instance, is there a need to declare a variable and what is the use of a module and so on. So this is tutorial is first of the series that I am writing. Now I will go for some bookish definition of VB.

General Information on Visual Basic 6.0
Visual Basic 6.0 is RAD tool. RAD means Rapid Application Development Tool. It is very easy to create a Data-Entry Form using VB. If it takes a 6 months for you to create a C++ application, it will take only 6 minutes for you to do it in VB. VB 6.0 has many powerful features that are required in today’s programming environment. Some of them are as follows

  1. The visual forms and controls like listbox and radio buttons that you use on the form help you to interact with the application in order to find out the flow of the program.
  2. The forms and objects perform a specified action when an event occurs. This is known as event-drive programming.
  3. Visual Basic includes many wizards that can automate tasks or even automate coding. The tools provided by VB help you reduce development time. It is faster to create applications using the tools available.

A visual basic project is made up of forms, objects, and code modules that the application needs. Visual basic project file have the extension *.vbp, Standard Modules in Visual Basic have the extension *.bas and Class Modules have the extension *.cls. Now I will get into the programming part of Visual Basic where we will create some small applications get to do some processing with Visual Basic. I am assuming you have some familiarity with the Visual Basic IDE.

Data Types in Visual Basic
Every application needs to use data to generate the required output. The application may be built to perform simple tasks like addition or for complex database management such as airline reservations or storing bank transactions. The Data-Types provided by Visual Basic is known as. Some of the fundamental data types provided by Visual Basic are as follows

  1. Byte
  2. Boolean
  3. Integer
  4. Long (long integer)
  5. Single (single-precision floating-point)
  6. Double (double-precision floating-point)
  7. Currency
  8. Date
  9. Object
  10. String (variable length)
  11. String (Fixed Length)
  12. Variant (with numbers)
  13. Variant (with characters)

Those are the data types used in VB. I will not be giving a detailed description as I feel I cannot do a good enough job on that. In any case we will be using mostly all the data types displayed, except the last one OLE_COLOR. Why, because I don’t know what heck it is and neither have I used it. Now we will come to


Variables are used to store data. It can be thought as a container in which data is stored. A variable essentially has two parts, a name, by which it is referred to and, a value. This value depends on the data type of the variable. One can declare a Variable using the Dim Statement.


Dim VarName As DataType

Var Name is the name of the variable and Type is Data Type of the variable.


Dim sVar1 as String

One can declare more than one variable in a single statement, for instance

Dim sVar1 as String, iVar2 as Integer

One need not specify the Data Type of the variable that one is declaring. In such a case, the variable is allotted the Variant data type and initialized to Empty. For instance

Dim varAnyVar
VarAnyVar = “Hello Nethaji” 'Now varAnyVar will store a string.
VarAnyVar = 10 'Now varAnyVar will store an integer.

The Variant data type offers benefits in terms of flexibility because you can assign any type of data to such a variable. However, it is a good programming practice to explicitly declare the type of the variable in order to avoid confusion and loss of data during conversions.

The String Declaration:

String variables can be declared of two types:

  • * Variable Length: A string variable is by default of variable length. This means that the size of the variable depends on the value that is assigned to it. For instance, if we assign the string “Hello” to a variable, the size will be 5 bytes. Then, if we assign “Hello Nethaji” the size will change to 13 bytes.Example:
          Dim sVar1 as String 'can store any number of characters
  • * Fixed Length: When we need to store a value of fixed length then we use Fixed Length string variables.Example:
          Dim sVarFixed as String * 100 'can store only 100 characters

Type Declaration Characters:
There is an alternative way of declaring a variable. You can declare a variable and specify the type of the variable by using type declaration character. A type declaration is a symbol that specifies the data type of the variable. In this case we do not have the As clause.

Dim iLocalVar% 'this declares iLocalVar as of Integer Type

A list of Type Declarations

  1. % denotes Integer
  2. $ denotes String
  3. @ denotes Currency
  4. & denotes Long
  5. # denotes Double
  6. ! denotes Single

In visual basic, certain rules and Naming Conventions have to be followed while specifying a name for a variable. I am not going delve into it, as you will already know what it is all about. But it is important that you name your variables appropriately and get used that because that’s the way professional program. If you name your variable appropriately then it will easier for you to identify the variable when maintaining them. As said earlier, one need not declare a variable as the default data type is taken as Variant and VB at runtime determines by checking the value. This concept is called implicit declaration. There’s nothing wrong in this method, but it is strongly recommended NOT to do this. When doing an implicit declaration there are possibilities that you might lose data. For instance, take this example.

intPrice = "3423"
intPrice = intPrice - (intPrice * 0.1)
Msgbox intPrice
'The output of this 3080.7, while if we declare the variable
Dim intPrice As Integer
intPrice = "3423"
intPrice = intPrice - (intPrice * 0.1)
Msgbox intPrice
'The output of this is 3081

One can clearly see the small data loss that has occurred when the runtime conversion was done. This might not be a serious threat for small scale application, but large scale applications need more protection from these kind of mistakes. Thus in order to ensure accuracy one needs to use Option Explicit in their projects. This can be enable by default. All you have to do is, Open Visual Basic Goto Tools and select Options. Enable the second check box which has the Text Require Variable Declaration.Now close visual basic IDE and open it again. Select a New project, you will notice that when you goto the code window, a line would’ve been added Option Explicit. This one line ensures that all variables are declared. Now to a particularly important part of Variable Declaration.

Scope of a Variable:

The scope of a variable determines the sections of an application in which the variable can be used. Visual basic offers four different types of a scope for a variable.

  1. Private: These variables are declared within a procedure and are local to the procedure.
  2. Module-level: These variables are declared using the keyword, Private, in a module and can be used by all the procedures in that module but not by procedures in other modules.
  3. Public: These variables are declared using the keyword, Public, and can be used by all the procedures in all the modules of a project.

Scope of a variable in forms and modules:


  1. When a variable is declared public here, it is accessible anywhere in the project. But the catch is that you have precede the forms name when accessing the variable (i.e.) FormName.Variable
  2. When a variable is declared private then its accessible only inside the form and not outside. You can declare a variable private by
    Dim iCnt as Integer (or) Private iCnt as Integer


  1. The first point similar to a module, except we don’t have to specify the module name
  2. Second point in forms is similar for modules

Class Modules

  1. Here if you declare a variable public, you will not be able to call it, unless and until you instanstiate the class. (E.g)
    Dim iClass As New Class1.
  2. Well if you declare private then it stays within the class module.

Scope Examples:

Private Sub SomeSub ()
    Dim iLocal As Integer
    iLocal =10
End Sub

In the above procedure’s case, the variable iLocal can be accessed only inside the procedure SomeSub ().

Dim iPublic as Integer

If this variable declared above all the coding and just below the Option Explicit, then it is said to have a global access. It can be accessed anywhere in the form. One important thing to note is that you cannot use scope qualifiers, such as Public or Private within a procedure. You need to use the Dim keyword within a procedure

Variables having the same name:

One can define more than one variable with the same name provide the scope of the variables is different. Therefore, the following cases are possible

  1. You can define variables with the same name in a module in which the scope of each variable is local.
  2. If you can declare a variable with the same name at the module level as well as within a procedure, the module-level variable has higher scope than that of the local variable. The module-level variable is said to have a higher level local scope and the procedure-level variable has a lower level local scope. In cases where you have defined a variable with the same name but with different scopes, the variable with the lower level scope is accessed when the variable name is referenced. This is known as shadowing.

For Instance:

'In the General Declaration Section if we Have
Option Explicit
Public iVar1 as Integer
'Then in a procedure if we have
Private Sub prcShadow ()
    Dim iVar1 as Integer
    IVar1=10 'local variable is being set value of 10 (shadowing)
    FormName.iVar1 = 100 'global variable is being set the value of 100.
End Sub

Static Keyword:
The Variables that are local to a procedure are destroyed once all the statements in the procedure are executed. However, in several cases, you might need to preserve the value of a variable after the procedure has been executed. This is especially useful if one needs to know the number of time a procedure has been called.

For Instance:

'Declare a static variable in a procedure
Private Sub prcStaticCheck()
    Static iCounter as Integer
    iCounter = iCounter + 1
    Msgbox "The Value of Counter is " & iCounter
End Sub
'Then in the same form load's event
Private Sub Form_Load ()
Call prcStaticCheck () 'It will display 1
Call prcStaticCheck () 'It will display 2
Call prcStaticCheck () 'It will display 3
Call prcStaticCheck () 'It will display 4
End Sub

As you can see the value is retained in the variable if we declare the variable as static in declaration part. If a procedure is declared static then all the variables within the procedure retain the value. Okay with this I am ending my first part. Hopefully you should’ve understood most of the stuff that I have mentioned in this part. If you think i’ve missed something something i’ve typed something wrong, please do correct me.

Leave a Reply

Your email address will not be published. Required fields are marked *