Making Decisions with Condition Blocks

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.

Pick an Option with If…Then…Else Statements

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

  1. In the Code window, create a new procedure by typing Sub IfThenCondition and pressing ENTER.
  2. Between the lines Sub IfThenCondition and End Sub, add the following line of code:
  3. 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.

  4. After the line containing the InputBox function, enter the following If…Then condition block:
  5. 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.

  6. Place the cursor anywhere in the IfThenCondition procedure and press F5. You'll see the InputBox prompting you to enter a number greater than 10. If the condition is evaluated to True (that is, the input value is a number greater than 10), the program displays the confirmation message. If the number is less than or equal to 10, no message is displayed.
  7. Enter a number greater than 10 and click OK.
  8. Click OK to close the message box.
  9. Add the following two lines between the line containing the MsgBox function and End If of the If…Then condition block you just created:
  10. 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.

  11. Press F5.
  12. Enter a number less than 10 and then click OK.
  13. Click OK to close the message box.
  14. Within the If…Then…Else condition block you created above, add the following two lines just above the line Else:
  15. 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:

    Click to view at full size.

  16. Press F5.
  17. Enter the number 10 and click OK.
  18. Click OK to close the message box.

Select Among Options with Select Case

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.

  1. In the procedure you created above, delete the If…Then…ElseIf condition block and replace it with a Select Case statement. The revised procedure looks like this:
  2. 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.

  3. Run the procedure and enter numbers as you did before. The procedure responds the same way.