VB 6 – If…Then…Else Statements

Advertisements
Advertisements

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 If

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

Example Program: If…Then…Else

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 Sub


If the number for variable day is greater than 7 , then default message is displayed – "Enter number less than equal to 7".

Advertisements

Output – If…Then…Else

The output of the above program is given below.

Output - If-Then-Else
Figure 1 – Output – If-Then-Else

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