HandsOn Using the IfThen Statement

This chapter's hands-on exercises are provided in the Acc2003_Chap05.mdb file included in the book's downloadable files.

1. Open Acc2003_Chap05.mdb from the book's downloadable files or, if you'd like to start from scratch, create a new Access 2003 database.

2. In the Visual Basic Editor window, choose Insert | Module to add a new module.

3. In the Code window, enter the SimplelfThen procedure shown below.

Sub SimpleIfThenO Dim weeks As String weeks = InputBox("How many weeks are in a year:", "Quiz") If weeks<>52 Then MsgBox "Try Again" End Sub

The SimpleIfThen procedure stores the user's answer in the weeks variable. The variable's value is then compared with the number 52. If the result of the comparison is true (that is, if the value stored in the variable weeks is not equal to 52), Visual Basic will display the message "Try Again."

4. Run the SimpleIfThen procedure and enter a number other than 52.

5. Rerun the SimpleIfThen procedure and enter the number 52. When you enter the correct number of weeks, Visual Basic does nothing. The procedure ends. It would be nice to also display a message when the user guesses right.

6. Enter the following instruction on a separate line before the End Sub keywords:

If weeks = 52 Then MsgBox "Congratulations!"

7. Run the SimpleIfThen procedure again and enter the number 52. When you enter the correct answer, Visual Basic does not execute the Try Again statement. When the procedure is executed, the statement to the right of the Then keyword is ignored if the result from evaluating the supplied

Part I

condition is false. As you recall, a VBA procedure can call another procedure. Let's see if it can also call itself.

8. Modify the first If statement in the SimplelfThen procedure as follows: If weeks <> 52 Then MsgBox "Try Again" : SimplelfThen

We added a colon and the name of the SimplelfThen procedure to the end of the existing If.. .Then statement. If the user enters the incorrect answer, he will see a message, and as soon as he clicks the OK button in the message box, he will get another chance to supply the correct answer. The user will be able to keep on guessing for a long time. In fact, he won't be able to exit the procedure gracefully until he supplies the correct answer. If he clicks Cancel, he will have to deal with the unfriendly "Type Mismatch" error message. For now, you may want to revise your SimpleIfThen procedure as follows:

Sub SimpleIfThen2() Dim weeks As String

On Error GoTo VeryEnd weeks = InputBox("How many weeks are in a year:", "Quiz") If weeks <> 52 Then MsgBox "Try Again":SimpleIfThen2 If weeks = 52 Then MsgBox "Congratulations!" VeryEnd: End Sub

If Visual Basic encounters an error, it will jump to the VeryEnd label placed at the end of the procedure. The statements placed between On Error GoTo VeryEnd and the VeryEnd label are ignored. In the last section of this chapter you will find other examples of trapping errors in your VBA procedures.

9. Run the SimpleIfThen2 procedure a few times by supplying incorrect answers. The error trap that you added to your procedure will allow you to quit guessing without having to deal with the ugly error message.

0 0

Post a comment