Logical operators perform conditional and, or, and not operations. Some languages support both binary logical operators that link two and unary logical operators negate (make opposite) the truth value of its argument. Finally, some languages short circuit logic. For example, with this or that, if this is an expression returning true, then that is never executed.
Access VBA Logical Operators
Same as VB. Access VBA logical operators:
and
and, as in this and that
or
or, as in this or that
Not
Not, as in Not This
Access VBA never short circuits. Given the expression this or that as well as this and that, if this evaluates to false, then that is still executed.
Syntax Example:
'Given expressions a, b, c, and d:
If Not (a and b) and (c or d) Then
'Do something.
End If
Using the Logical Not Operator
You can use the not operator in many contexts. One of my favorite uses for it is to toggle boolean properities with a single line of code:
BooleanProperty = Not BooleanProperty
MS Access 2003 Working Demo
The following demo uses the not operator to toggle the visible property of a Image object from a button.
Create a new form and place a button and a Image on it. Add a picture to the Image with the Picture property.
Edit the Click event of the button and alter it as follows below. Select the button and edit the On Click event on the Event tab of the Properities explorer. Your Image object should be named Image1, but if it's not, use the correct object name.
Private Sub Command1_Click() Image1.Visible = Not Image1.Visible End Sub
Run the form and test. Select View | Form View and click the button. You'll notice the picture toggles between visible and not visible with a single line of code.
Access VBA Short Circuting Example:
In the following example, if Access VBA supported short circuting, the That function would never execute.
Function This() MsgBox ("The This function executed.") This = False End Function
Function That() MsgBox ("The That function executed!") That = True End Function
Private Sub Command0_Click() 'Notice both the This and That functions execute 'even though the This Function returned False. If This And That Then MsgBox ("hi") End If End Sub
The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.