Access VBA Macro Programming

(Joao Candeias) #1
Conditional operators that can be used are as follows:

Operator Meaning
= Both numbers or values are equal. This condition will also work for values, such as
“dog” and “cat.”
< First value is less than second value.
> First value is greater than second value.
<= First value is less than or equal to second value.
>= First value is greater than or equal to second value.
<> First value is unequal to second value.

An expression such asx=1is evaluated as a Boolean value, which is True or False or
Non-zero or Zero. This means you do not always have to use an operator. If you are only
interested in whether a variable has a non-zero value in it, then you can use

If MyVar Then MsgBox "MyVar has a value"

Multiple Conditional Statements


In the preceding statements, I used only a single conditional statement in the form ofIf x=1
Then.... You can also use multiple conditional statements using a logical operator. For more
information on logical operators, refer to Chapter 6.
Multiple conditional statements are straightforward and work almost like plain English.
They use the operatorsAndandOrand, 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 precedingOr, 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.

38 Microsoft Access 2010 VBA Macro Programming

Free download pdf