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:
and, or, not
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.
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.
In the following example, if ASP Classic supported short circuting, the That function would never execute. It's interesting to note that VB.Net has introduced two new operators to support short cicuiting: AndAlso and OrElse for use when you code in ASP.Net.
<body> <h1>Short Circuting</h1> <% Function This() Response.Write("<br>The This function executed.") This = False End Function
Function That() Response.Write("<br>The That function executed!") That = True End Function
'Notice both the This and That functions execute 'even though the This Function returned False. If This And That Then Response.Write("<br>Both are true.") End If %> </body>
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.
Like VBA, ObjectPAL 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.
The following code snippet allows you to test out ObjectPAL's short circuiting. In the if statement of the pushButton event, the That() custom procedure executes even if This() is False.
To use the code, alter a pushButton event of a button as follows:
;though This() returns false. if This() and That() then msgInfo("", "both are true") endIf endMethod
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
Working Paradox Demo
The following demo works in all versions of Paradox for Windows. The demo uses the not operator to toggle the visible property of a box from a button.
Create a new form and place a button and a box on it.
Name the box "TheBox". To rename it, select the box, right click and click properties. In the Name of object field, change the name to MyBox.
Edit the pushButton event and alter it as follows below. Select the button, right click, click Object Explorer. Double click the pushButton event on the Events tab.
method pushButton(var eventInfo Event) TheBox.Visible = Not TheBox.Visible endMethod
Run the form and test. Select Program | Run and click the button. You'll notice the visible box toggles between visible and not visible with a single line of code.
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.
The Delphi compiler default is to short circuit multi argument boolean expressions when the result is known before the evaluation completes. To disable short circuiting, use the {$B+} compiler directive. To reset it back to the compiler default of short circuting, use the {$B-} compiler directive.
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.
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.
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
VB6 Working Demo
The following demo uses the not operator to toggle the visible property of a PictureBox from a button.
Create a new form and place a button and a PictureBox on it. Add a picture to the PictureBox with the Picture property.
Edit the Click event and alter it as follows below (double click the button). Your PictureBox should be named Picture1, but if it's not, use the correct object name.
Private Sub Command1_Click() Picture1.Visible = Not Picture1.Visible End Sub
Run the application and test. Select Run| Start and click the button. You'll notice the picture toggles between visible and not visible with a single line of code.
VB Classic Short Circuting Example:
In the following example, if VB Classic supported short circuting, the That function would never execute. It's interesting to note that VB.Net has introduced two new operators to support short cicuiting: AndAlso and OrElse but you'll have to move to VB.Net to take advantage of them.
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.
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.