In any production process, whether you're producing a report, a car, or software, you need to decide what materials you need and how to control the flow of those materials through your personnel, your departments, and your technologies. At each point in the process, you may gather input from sources like quality control or computer sensors and select alternative flows for the material. In Visual Basic, you decide what data you need to work with, create a program flow to work with the data, gather input from users or data sources on your system or network, and produce a result.
In many situations, you select a path depending on conditions, just like on a drive from city to city. When travelling, you choose which roads to take depending on weather, traffic, scenery, and time. Your Visual Basic program is no different. Once you decide what conditions determine the flow, your program will follow a logical path to the final output. Visual Basic provides a number of syntax choices to allow you to evaluate information and run appropriate code, depending on which criteria and conditions you specify.
In Visual Basic the most common and simplest condition statements are If…Then and If…Then…Else. These statements allow you to evaluate a condition (or set of conditions) in order to run a particular block of statements. (You already used this structure in the procedure you wrote to determine a data type.)
sInput = InputBox("Enter a number greater than 10.") |
As discussed earlier, the built-in Visual Basic InputBox function prompts the user to enter a value in a text box and returns a string representing the value that the user enters. In this case, you prompt the user to enter a number greater than 10.
If sInput > 10 Then MsgBox "You entered a number greater than 10." End If |
Within this If…Then condition block, if the value entered by the user is greater than 10, the program displays the message box. Otherwise, nothing else happens.
Else MsgBox "You entered a number less than 10." |
The If…Then…Else condition block goes one step further. It provides an alternative if the condition isn't met. Now, if you enter a number less than 10, you see a different message box.
ElseIf sInput = 10 Then MsgBox "You entered the number 10." |
Finally, the If…Then…ElseIf condition block goes yet another step and adds more flexibility than the If…Then…Else condition block. With the If…Then…ElseIf condition block, you can evaluate more than one condition within the same block. Now, a different message box is displayed for all three cases for the input value. This value can either be greater than, equal to, or less than 10.
The completed If…Then…ElseIf condition block should look like this:
The Select Case condition block is very similar to the If…Then…ElseIf condition block. Select Case evaluates the test expression in the first line and afterward compares it to the value in each case.
Sub IfThenCondition() sInput = InputBox("Enter a number greater than 10.") Select Case sInput Case Is > 10 MsgBox "You entered a number greater than 10." Case Is = 10 MsgBox "You entered the number 10." Case Is < 10 MsgBox "You entered a number less than 10." End Select End Sub |
Pay particular attention to the similarities between the overall structure of the Select Case condition with the If…Then…ElseIf condition block. In the Select Case condition block, the variable sInput is the test expression and you enter it at the beginning of the condition block. Then a number of Case statements follow to handle the possible conditions of the test expression.