In the previous article, you have learned about the if-then-else statements and how you can build a program in VB 6 that work on conditions. The problem with if-then-else structure is that it is very difficult to keep a track of all codes.
The Select-Case is a VB 6 feature that allows to select a case when the condition is met and execute a code. It is a more simplified version of if-then-else statements.
The syntax for Select-Case is given below.
Select Case expression
Case 1:
some expression;
Case 2:
some expression;
Case 3:
some expression;
Case 4 To 8:
some expression;
Case Is <= Total:
some expression;
Case Else:
some default expression;
End SelectSelect Case ‘expression‘ – Here the Select Case wants a variable or an expression to check if any Case match with it. If there is a match then execute codes under that case.
Case ‘value’ – A case has a number or expression that evaluates to a number. The code under case is executed when the Select Case condition is met.and its value match with the Case value.
End Select – This terminates the Select Case block.
Each of the case has a value associated with it. It may be in the form of number, or an expression.Let us see few examples below:
Case 4: //Case with number and a colonCase 4 To 10: // Gives option to choose number between 4 and 10Case Is >= 20: //Case condition evaluates to true, and code gets executedWhen the case does not match, the last case is Case Else which executes the default statement.
Private Sub Command1_Click()
Dim physics_mark As Integer, maths_mark As Integer
Dim english_mark As Integer, average As Integer
Dim result As String
physics_mark = Val(Text3.Text)
maths_mark = Val(Text2.Text)
english_mark = Val(Text1.Text)
average = (physics_mark + maths_mark + english_mark) / 3
Select Case average
Case Is <= 50:
Text4.Text = "You Passed"
Case Is <= 70:
Text4.Text = "Good Marks"
Case Is <= 90:
Text4.Text = "Top Student"
Case Is < 100:
Text4.Text = "Exceptional"
Case Else
Text4.Text = "Sorry! You Failed"
End Select
End SubIn the above program, students enter marks for 3 subjects – English, Mathematics, and Physics. The average of the marks is computed and using Select-Case the appropriate result is displayed.

The conditional statements are very important to any programming language. In VB 6, the conditional statement change the direction of the program using If...Then ...Else construct.
The syntax for If...Then...Else is given below.
If condition Then
[Statements]
Else If condition 1 Then
...
[statements]
...
Else If condition n Then
...
[Statements]
...
Else
...
[Statements]
...
End IfIf you look at the above syntax , the conditions are very important for If statements. If the condition 1 is false then another condition is checked and so on. The condition is met (True), then that statement block is executed. If no condition is met, then the last Else statement is executed.
Consider the following example code, the program has a variable called day. If the day is 1, it is Monday, else it is Tuesday, and so on.
Private Sub Text1_Change()
Dim day As Integer
day = Val(Text1.Text)
If day = 1 Then
Text2.Text = "Monday"
ElseIf day = 2 Then
Text2.Text = "Tuesday"
ElseIf day = 3 Then
Text2.Text = "Wednesday"
ElseIf day = 4 Then
Text2.Text = "Thrusday"
ElseIf day = 5 Then
Text2.Text = "Friday"
ElseIf day = 6 Then
Text2.Text = "Saturday"
ElseIf day = 7 Then
Text2.Text = "Sunday"
Else
Text2.Text = "Enter Number Less Than Equal To 7"
End If
End SubIf the number for variable day is greater than 7 , then default message is displayed – "Enter number less than equal to 7".
The output of the above program is given below.

In Visual Basic 6, there are two important functions to change a string to uppercase or lowercase. In this example, you will build a small application that takes a string and returns uppercase or lowercase version depending on user choice.
First look at the code for first button – UpperCase.
Private Sub Command1_Click()
Text2.Text = UCase(Text1.Text)
End SubNow code for button – LowerCase.
Private Sub Command2_Click()
Text2.Text = LCase(Text1.Text)
End Sub
Earlier you learned about the string data type. The VB 6 language has many functions to handle string types. In this article, we will present the list of those functions with examples.
There are two types of strings in VB 6 programming.
To declare variable length string you can use following syntax.
'variable length string declaration
Dim myStr as String
'fixed length string declaration
Dim myFixedStr as String * 20You can notice that we are using asterisk (*) to specify the length of the string which is 20 characters.
| String Functions | Description |
| StrComp() | Compares two strings |
| StrConv() | Convert strings |
| Format(), LCase, UCase | Convert A string to lowercase or uppercase |
| Space(), String() | Create strings |
| Len() | Get the length of the string |
| Format | Format a string |
| LSet, RSet | Justify a string |
| InStr, Left, Mid, Right, Trim, LTrim, RTrim | Manipulate strings |
| Option Compare | string comparison values |
| Asc, Chr | Use ASC II and ANSI values |
A subroutine is a block of code like function that do a specific task. The difference between subroutine and function is that a subroutine does not return a value. However, you may pass arguments to a subroutine in VB 6.
The syntax for subroutine is given below, the keywords within square bracket ([]) are optional.
[Private | Public | Friend] [Static] Sub-name [(argument list)]
...
[Statements]
...
[Exist Sub]
...
[Statements]
...
End SubPublic keyword allows procedures from all forms and modules to access the public procedures.
Private keyword only allows access to procedures from forms and modules where private procedure is declared.
Friend keyword used in class modules (modules that implement classes) allows the procedure to be visible throughout the project except to the controller of the instance of an object.
Static keyword preserve the value of local variable (those declared inside the procedure) between calls. You can call a procedure several times, it would not lose the value of a static local variable.
Argument list is a list of variables that it passed to the procedure. You can have multiple arguments each separated by a comma.
Argument list has following syntax:
[Optional] [ByVal | ByRef] [ParamArray] varname [()] [As type] = [default value]Optional means that the argument is not required.
ByVal means the argument is passed to the procedure by value. ByRef means that the argument is passed as a reference rather than the value.
ParamArray is the last argument in the argument list and it indicates that the final argument is an array of Variant elements.
Varname is the name of the argument being passed to the procedure.
type is the data type of the argument.
defualt value is a constant or a constant expression set when you use the [Optional] keyword.
Exit Sub cause the sub procedure to immediately exit itself.
End sub is to end the sub procedure.
Here is an example of sub procedure.
Sub Matrix (Optional MatValues as Variant)
If IsMissing (MatValues) Then
' If value is not passed
MsgBox("Value not passed to the Matrix")
Else
...
End If
End SubIn the next article, we will learn about VB 6 function which is similar to sub procedure.
The VB 6 arrays hold data of a single type. In this article, you will learn about declaring, assigning, and various array handling techniques.
You can divide VB 6 arrays into two sets –
array and
arrays The
can have a maximum of
dimensions. The arrays in
are also classified based on keywords used to declare them:
The regular arrays are the usual way of declaring an array using the Dim statement. The syntax to declare a regular array is given below:
Dim [WithEvents] variable_name [(Subscripts)] [As [New] type] [, [WithEvents] variable2_name [(Subscripts)] [As [New] type] ...[lower To] upperExample:
TotalAmount( 10 To 20)Example:

Assigning Values to Regular Array Elements
To assign values to array elements refer to them using an index and then assign value like any normal variable.
For Example:
Private Sub BtnTax_Click()
Dim salary(10) As Double
Dim Location(10) As String
Location(9) = "Nottingham"
Location(7) = "France"
Print Location(9)
Print Location(7)
salary(2) = 2003
End SubIn the next section, we will discuss declaring dynamic arrays and assigning values to them.
A dynamic array does not have a fixed size, it is expanded dynamically. The Dim statement is used to declare an array without any subscript value (between the parentheses) .
For example,
Dim Accounts ( ) As Integer
Dim Employee( ) As StringThe next step after declaring the array is to set the length of the array. Since this is a dynamic array we could increase the size multiple times.
The syntax:
ReDim [Preserve] variable_name (subscript) [As type]Example:

The Array ( ) function is a special function in Vb 6 that takes array values and directly assign it to a variant type variable. The variant type variable becomes an array of the type supplied in the argument list.
Syntax:
Array(argument list)Example:
Dim Tot As Variant
Tot = Array(23, 44, 56)
' Output the last element of the array, the size of the array is set automatically
Print Tot(2)Here is a list of array handling statements and functions provided by VB 6.
| Statements or Functions | Description |
| Dim, Public, Static, ReDim, Private | Declare or Initialize an array |
| Erase, ReDim | Reinitialize an array |
| Array | Create an Array |
| IsArray | Verify an array |
| LBound, UBound | Find the upper or lower limit of an array |
| Option Base | Change default lower limit 0 to a non-negative integer value |
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.
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:
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.
Example:
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.

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.
Example:
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.

Procedure-Level
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.
Example:

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.
Type casting in VB 6 means converting from one data type to another. The VB 6 language allows type conversion using functions.
Here is a list of functions with their description.
| Function | Description |
Chr | ANSI to string |
Format, LCase, UCase | String to Lowercase or Uppercase |
DateSerial, DateValue | Date to Serial Number |
Hex, Oct | Decimal to Hexadecimal or Oct |
Format, Str | Number to Str |
CBool, CInt, CByte, CLng,CCur, CDate, CDec, CSng, CVar, CStr,CVErr,CDbl, Fix, Int | Convert from a data type to another |
Day, Month, Weekday, Year | Date to Day, Month, Weekday, or Year |
Hour, Minute, Second | Time to Hour, Minute, or Second |
Asc | String to ASCII value |
TimeSerial, TimeValue | Time to Serial number |
Val | String to number |
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.
In this article, you will learn to declare variables and constants. We will learn rest of topics in future articles.
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:
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
VariantThe default type is variant which takes value of any data type.
For example,
Dim Car As String
Dim Price As IntegerMultiple Variable Declaration
You can declare more than one variables at a time using the Dim statement.
Syntax:
Dim [WithEvents] variable1 name [([subscripts])] [As [New] type], [WithEvents] variable2 name [([subscripts])] [As [New] type], ...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.

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.

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.

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.
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] = expressionPrivate Sub Command1_Click()
Const side = 2.5
Dim Area
Area = side * side
MsgBox ("Area = " + Str(Area))
End SubWhen the button is clicked it will output area, but the constant value side will not be altered.
Visual basic open a project in design view where you can modify project properties like project name and description, version information, and other similar attributes. Managing VB 6 projects is easier than you may think.
The project tab in the visual basic standard toolbar contains project properties which allow us to change settings related to a visual basic project.
You can also add predefined forms, menus, and modules to your project using the visual component manager. A menu in the visual component manager can be reused in many different forms. Not an only menu, but you can add other types of components like class, modules, media forms, reports, documents, etc.
To name a visual basic project you must:
Example:
We want to set the project name as MyFirstVBProgram. To set this name, go to project tab > select project properties > under general tab > project name, type the name for your project and a description under project description section.

Larger vb projects need to keep version numbers and version information such as company name, software product name, license information, copyright information, legal trademarks, comments, etc.
You can set this information from project properties > make tab. The two options related to the project are:
You can enter the project version number and other required details under version description. The project icon is the icon set for the startup form. Jump to next section for information on startup form.

After you convert your project into an EXE file (File menu > make MyFirstVBProgram.exe), right-click the file and go to properties > details tab. You can see the version information related to the project.

The startup form is the first form loaded in when the visual basic starts. You can decide the form or the object to load at startup in visual basic.
To set this value, go to project properties, under general tab> select the startup object from the drop-down and click ok.

In the figure above, the startup object is Form1 and loaded first.
Managing visual basic 6 projects needs two types of components frequently.
The predefined menu is added using visual basic component manager. If you have not installed the component manager, use the add-in tab to load it.

Under add-ins > click add-in manager and select the visual component manager 6.0 to install. If already installed, go to standard toolbar.
You can add the components by clicking on the visual component manager from the standard toolbar icon. This will open the window for the visual component manager.

Predefined Menu for Form
When the window for component manager is open, click Visual basic folder > open Templates folder and select the folder for predefined component you want to add to vb project.
For example, you want to add a File menu to the project form. Then select Menu folder under Templates in the visual component manager.

If nothing is found, create a new component by clicking on the “publish a new component” option on the top and finished the new component wizard.
Now you can select a new reusable component for your project.

Add ActiveX Controls
There are special controls called ActiveX that require that visual basic loads the necessary files. For example, you can connect SQL database to vb form using data grid control.
To enable the activex controls, go to project > components and select the components you want to install.

The tab insertable objects, let VB programmers insert MS Excel, MS Word, and other similar objects.