Connditionals
 

If statements

firstname = "Phil"

If firstname = "Bill" Then
          MsgBox "firstname is Bill"
Else
           MsgBox "firstname is not Bill"
End If


Start a new project. Add a textbox, a Label and a button to your new Form. Then write a programme that does the following:

Asks users to enter a number between 10 and 20.
The number will be entered into the Textbox.
When the Button is clicked, your Visual Basic code will check the number entered in the Textbox.

If it is between 10 and 20, then a message will be displayed.

The message box will display the number from the Textbox.

If the number entered is not between 10 and 20 then the user will be invited to try again, and whatever was entered in the Textbox will be erased


The Select Case statement

another way to test what is inside of a variable. You can use it when you know there is only a limited number of things that could be in the variable.

Dim cream As String
Dim Diet As String
cream = TextBox1.Text

Select Case cream

Case "Eaten"

Diet = "Diet Ruined"

Case "Not Eaten"

Diet = "Diet Not Ruined"
Case Else

Diet = "Didn't check"

End Select

 


> means Is Greater Than

If number > 10 Then
MsgBox "The Number was Greater Than 10"
End If



< means Is Less Than

If number <10 Then
MsgBox "The Number was Less Than 10"
End If



>= mean Is Greater Than or Equal to

If number >= 10 Then
MsgBox "The Number was 10 or Greater"
End If



<= mean Is Less Than or Equal to

If number <= 10 Then
MsgBox "The Number was 10 or Less"
End If



And - You can combine the logical operators

If number > 5 And number < 15 Then
MsgBox "Greater than 5 And Less than 15"
End If


Or - combine the logical operators

If number > 5 Or number < 15 Then
MsgBox "Greater than 5 Or Less than 15"
End If



<> mean Is Not Equal to

If number1 <> number2 Then
MsgBox "number1 is not equal to number2"
End If

(c) Shilpa Sayura Foundation 2006-2017