Selection Control Structures Selection control structures are used when you want to make a decision based on a value. The simplest form is the "If" statement. An "If" statement test the truth of a condition. If the condition is true, the code is executed. If x = 5 Then y = 10 End If The "End If" statement marks the end of the code to be executed. You may have more than one line of code to execute when the statement is true. Conditions must always evaluate to true or false. A condition is a comparison between two variables, or a variable and a literal. When comparing, use the conditional operators listed on VB245, Figure 4-7. Special care must be taken when comparing Strings. You don't know what the capitalization will be for a variable, it depends on how the user types it in. You, the programmer, have control over the literal side of the comparison. The comparison will always be case sensitive. If strName = "joe" Then strName could be "Joe", "jOe", "JOE", or any other variation Use the UCase or LCase function to exercise control over both sides of the comparison If UCase(strName) = "JOE" Then If LCase(strName) = "joe" Then The "If" executes code only when the condition is true. To execute code when the condition is false, use the "Else" clause: If x = 5 Then y = 10 Else y = 2 End If You may have more than one line of code to execute when the statement is false. Refer to page VB245, Figure 4-6, for the syntax of the If-Then-Else structure "If" statements can get complicated in two ways: Compound conditions This occurs when you want to test more than one condition. It uses logical operators and familiarity with truth tables. And: All condition must be true Or : At least one condition must be true Not: Reverses the truth of the condition It's often helpful to enclose each condition in parentheses to make it easier to read If ( x = 5 ) And ( y = 2 ) Then If ( x = 5 ) Or ( y = 2 ) Then If ( x = 5 ) And Not ( y = 2 ) Then Avoid the following whenever possible: Use of the Not Mixing And with Or Nested If This occurs when you have an If inside of another If, or inside an Else: If x = 5 Then If y = 2 Then z = 7 End If End If These are best avoided if at all possible. "If" statements are good when you want to make a choice between two different paths. But, what if you want to choose among more than two? Use the "Select Case" statement. An example of a "Select Case" statement that will assign a message based on a letter grade: Select Case strGrade Case "A" strMessage = "Excellent" Case "B" strMessage = "Good" Case "C" strMessage = "Fair" Case "D" strMessage = "Poor" Case "F" strMessage = "Failed" Case Else strMessage = "Unknown Value" End Select The "End Select" statement marks the end of the "Select Case" structure. Each path you want to take is a case based on the value of your variable. The "Case Else" covers the case when the value does not match any of the other cases. You may have more than one line of code to execute for each case. Each Case above is an implied equality test. You can use any of the other comparison operators. Refer to page VB263, Figure 4-27 for the syntax of a "Select Case". This is the "Select Case" I use in my database when computing letter grades: Select Case dblGrade Case Is >= 93 strRetVal = "A" Case Is >= 90 strRetVal = "A-" Case Is >= 87 strRetVal = "B+" Case Is >= 83 strRetVal = "B" Case Is >= 80 strRetVal = "B-" Case Is >= 77 strRetVal = "C+" Case Is >= 73 strRetVal = "C" Case Is >= 70 strRetVal = "C-" Case Is >= 67 strRetVal = "D+" Case Is >= 63 strRetVal = "D" Case Is >= 60 strRetVal = "D-" Case Else strRetVal = "F" End Select Classroom Exercises: On pages VB246-261, follow the directions in the beige boxes for Examples 1-5 On pages VB264-270, follow the directions in the beige boxes for Examples 1-3