Capture Errors with Case Else

Although using Case Else in the Select Case statement isn't required, it's always a good idea to include one just in case the variable you are testing has an unexpected value. The Case Else is a good place to put an error message.

Part I

Using Is with the Case Clause

Sometimes a decision is made based on whether the test expression uses the greater than, less than, equal to, or some other relational operator (see Table 5-1). The Is keyword lets you use a conditional expression in a Case clause. The syntax for the Select Case clause using the Is keyword is shown below.

Select Case testExpression Case Is conditionl statements if conditionl is true Case Is condition2

statements if condition2 is true Case Is conditionN

statements if conditionN is true

End Select

For example, let's compare some numbers:

Select Case myNumber Case Is <= 10

MsgBox "The number is less than or equal to Case 11

MsgBox "You entered 11." Case Is >= 100

MsgBox "The number is greater than or equal Case Else

MsgBox "The number is between 12 and 99."

End Select

Assuming that the variable myNumber holds 120, the third Case clause is true, and the only statement executed is the one between the Case Is >= 100 and the Case Else clause.

Specifying a Range of Values in a Case Clause

In the preceding example you saw a simple Select Case statement that uses one expression in each Case clause. Many times, however, you may want to specify a range of values in a Case clause. You do this by using the To keyword between the values of expressions, as in the following example:

Select Case unitsSold Case 1 To 100

Discount = 0.1 Case 501 To 1000

Discount = 0.15 Case Is >1000

Discount = 0.2

End Select

Let's analyze the above Select Case block with the assumption that the variable unitsSold currently has a value of 99. Visual Basic compares the value of the variable unitsSold with the conditional expression in the Case clauses. The first and third Case clauses illustrate how to use a range of values in a conditional

Introduction to Access 2003 VBA Programming expression by using the To keyword. Because unitsSold equals 99, the condition in the first Case clause is true; thus, Visual Basic assigns the value 0.05 to the variable Discount.

Well, how about the second Case clause, which is also true? Although it's obvious that 99 is less than or equal to 500, Visual Basic does not execute the associated statement Discount = 0.1. The reason for this is that once Visual Basic locates a Case clause with a true condition, it doesn't bother to look at the remaining Case clauses. It jumps over them and continues to execute the procedure with the instructions that may follow the End Select statement.

To get more practice with the Select Case statement, let's use it in a function procedure. As you recall from Chapter 4, function procedures allow you to return a result to a subroutine. Suppose a subroutine has to display a discount based on the number of units sold. You can get the number of units from the user and then run a function to figure out which discount applies.

0 0

Post a comment