If Statement in Visual Basic

In computer programming, a conditional statement is the type of statement that executes a command when the condition is met or when the condition is true.

The if…then statement

The if then statement of visual basic is similar to the if statement of other programming languages such as PHP and Java the only difference is that visual basic uses then and not “}”.

The if statement is the most common conditional statement used in programming, specifically in visual basic.

The if then statement check or validate if the condition is true. There are two forms on how to write the if then statement.

The single line method:

If yourCondition Then codeToExecute

Where:

yourCondition is an expression or combination of expressions (Boolean value or logical).

codeToExecute is the statement or code to be executed if the condition is true.

Note: in the single line method of if then statement End If is not required.

Example:

Private Sub Command1_Click()
If Text1.Text = "admin" Then MsgBox "Welcome admin"
End Sub

In the example, the if then statement will only be executed if the Command1 button will be clicked. The condition to be tested is: if the value of the Text1 is admin then the message box will prompt with the message Welcome admin.

The multiple line method:

Use this type of method if you have multiple line of codes to be executed.

The format is:

If yourCondition Then
codeToExecute1
codeToExecute2
codeToExecute3
End If

Note: in this method End If is required to end the statement

Example:

Private Sub Command1_Click()
If Text1.Text = "admin" Then
MsgBox "Welcome admin"
Form1.BackColor = vbRed
End If
End Sub

The same with the previous example, the statement will be executed when command button is clicked. There are two lines of code to be executed if the value of the Text1 is admin: the first one will display a message Welcome admin and the second one will make the background of the form into red.

if…then…else statement

The if then statement only evaluates if the condition is true, if the condition is true then it will execute the line(s) of codes but if the condition is false the if then statement has nothing to execute.

To be able to execute something if the condition is false, use the if then else statement.

Here is the format of if then else statement:

If yourCondition Then
codeToExecuteIfTrue
Else
codeToExecuteIfFalse
End If

Where:

codeToExecuteIfTrue is the line to be executed if the condition is true.

codeToExecuteIfFalse is the line to be executed if the condition is false.

Example:

Private Sub Command2_Click()
If Text1.Text = "admin" Then
MsgBox "Welcome admin"
Else
MsgBox "Welcome visitor"
End If
End Sub

In this example, our condition is Text1.Text = “admin”, if the user will enter admin in the textbox then the condition becomes true, thus the program will prompt a message Welcome admin, but if the user will enter another value the condition becomes false and the program will prompt Welcome visitor.

We can also add multiple statements to be executed (both on true and false condition).

Here’s an example:

Private Sub Command2_Click()
If Text1.Text = "admin" Then
MsgBox "Welcome admin"
Form1.BackColor = vbRed
Else
MsgBox "Welcome visitor"
Form1.BackColor = vbGreen
End If
End Sub

if…then…elseif statement

the if then statement and the if then else statement has only one condition to test, so what if you want two or more conditions to be tested? What should you do?

The answer is simple, use the if then elseif statement. With this statement you can test two or more conditions. Here’s how:

If yourCondition1 Then
codeToExecute1
ElseIf yourCondition2 Then
codeToExecute2
ElseIF yourCondition3 Then
codeToExecute3
Else
codeToExecute4
End If

Where:

yourCondition1 is the first condition to be tested, if it’s true then codeToExecute1 will be executed and disregard the rest.

yourCondition2 is the next condition to be tested if yourCondition1 is false, if yourCondition2 is true then the codeToExecute2 will be executed.

yourCondition3  is condition to be tested if yourCondition1 and yourCondition2 are false, if yourCondition3  returns true then codeToExecute2 will be executed.

The Else in this statement is very important because if none of your conditions is true then codeToExecute4 will be executed.

Note: you can add as many elseif statements as you want.

Example:

Private Sub Command3_Click()
If Text1.Text = "admin" Then
MsgBox "Welcome admin"
ElseIf Text1.Text = "guest" Then
MsgBox "Welcome guest"
Else
MsgBox "Welcome visitor"
End If
End Sub
, , , ,

Post navigation

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.