MS-Access / Getting Started

Multiple Conditional Statements

In the preceding statements, I used only a single conditional statement in the form of If x=1 Then.... You can also use multiple conditional statements using a logical operator.

Multiple conditional statements are straightforward and work almost like plain English. They use the operators And and Or and, for the purposes of this example, mean exactly what they mean in English.

If you have two conditions that you want to test, you write the If statement in the following form:

If x = 1 And y > 5 Then
	MsgBox "x=1 and y>5"
End If

The message box will be displayed only if both conditions (x = 1 and y > 5) are met. If, for instance, x > 1, but y has a value of 4, the message box will not be displayed. Similarly, you could use the following statement:

If x = 1 Or y > 5 Then
	MsgBox "x=1 or y>5"
End If

In the case of the preceding Or, the message box will be displayed if either one of the conditions is met. For example, if x = 1 or y > 5, the message box will be displayed. Therefore, x could be 0 and y could be 6, or x could be 1 and y could be 4, and the message box would still be displayed in either case.

You can put in several Ands or Ors within the condition, although it gets complicated with more than three. It all depends on what you are trying to achieve in your decision statement and what the procedure is trying to do. You may be writing something very simple such as If x=1 Then, or you may be working on a more complicated conditional statement.

[Previous] [Contents] [Next]