IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
Access
Search Access Group:

Advanced
-Collapse +Expand Access To/From
To/FromCODEGuides
-Collapse +Expand Access Store
PRESTWOODSTORE

Prestwood eMagazine

January Edition
Subscribe now! It's Free!
Enter your email:

   ► KBDesktop Data...Access & VBAAccess VBALanguage Basics   Print This     
  From the January 2016 Issue of Prestwood eMag
 
Access Language Basics:
Access VBA Logical Operators (and, or, not)
 
Posted 10 years ago on 2/11/2009 and updated 9/15/2009
Access Code Snippet:
 A flashcard from our Access Flashcards Library
 A code snippet from our Access Code Snippets Page

KB101890



Languages Focus: Logical Operators

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.

  1. Create a new form and place a button and a Image on it. Add a picture to the Image with the Picture property.
      
    Demo of using not operator.
     
  2. 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

     
  3. 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

More Info


Comments

1 Comments.
Share a thought or comment...
Comment 1 of 1

In your question one your dim statements for a, b and c will be variants. Only D is declared as a boolean.

You need to explicitly state the data type each time otherwise it will be a variant

---
Paul Arrowsmith
Posted 35 months ago
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1212A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile


Linked Certification Question(s)

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.

Beginner

1 Beginner Level Question

Question #1: True or False?

Given the following code, which MsgBox will display? The True or the False one?

Dim a, b, c, d As Boolean
  
a = True
b = True
c = True
d = False
 
If Not (a And b) And (c Or d) Then
  MsgBox ("True")
Else
  MsgBox ("False")
End If
Answer:
  • True
  • False
  • Intermediate

    1 Intermediate Level Question

    Question #2: Yes or No?

    Given the following two functions:

    Function This()
        This = False
    End Function
     
    Function That()
        That = True
    End Function

     

    Will the That function in the following code execute?

    Private Sub Command0_Click()
        If This And That Then
            MsgBox ("hi")
        End If
    End Sub
    Answer:
  • Yes
  • No

  •  KB Article #101890 Counter
    25276
    Since 2/11/2009
    -
       Contact Us!
     
    Have a question? Need our services? Contact us now.
    --Mike Prestwood

    Call: 916-726-5675

    email: info@prestwood.com


    Go ahead!   Use Us! Call: 916-726-5675 


    ©1995-2019 Prestwood IT Solutions.   [Security & Privacy]