Results 1 to 4 of 4
  1. #1
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    281

    Help with If OR...AND Statement


    Really wish I could figure this stuff out on my own - but I can't help to think Access is a contributor to my frustration - but I digress

    Simple If Or...And statement that wont work - no matter what.

    Setup: Userform with 2 Combo's (cboAssoc & cboTeam) - Entering a value in one will set the other to an empty string ("")

    If neither Combo has a value then the entire population of data is pulled for whatever date range is selected.

    The following two lines work perfectly:
    Code:
    If IsNull (Me!cboAssoc) Or Me!cboAssoc = "" Then
       Do Something
    End If
    
    If Me!cboAssoc <> "" Then
       Do Something
    End If
    Neither of the following will work at all: The first one is the one I would really like to use as it is (supposed to) check if either ComboBox (cboAssoc & cboTeam) is devoid of any value
    Code:
    If IsNull (Me!cboAssoc) Or Me!cboAssoc = "" AND IsNull (Me!cboTeam) Or Me!cboTeam Then
       Do Something
    End if
    
    I gave up on the above and tried using singular If statements such as below - nope, wont work
    
    If Not IsNull (Me!cboAssoc) Or Me!cboAssoc <> "" Then
       Do Something
    End if
    WTH am I doing wrong? Ugh!!

    Thanks

  2. #2
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,407
    Try it this way:

    Code:
    If (IsNull (Me.cboAssoc) Or Me.cboAssoc = "") AND (IsNull (Me.cboTeam) Or Me.cboTeam = "") Then
       Do Something
    End if
    Notice dot replacing bang. The red was missing in your code.
    The parens help Access understand how to interpret the AND mixed with the ORs

    Another approach:
    Code:
    If Len(me.cboAssoc & "") = 0 AND Len(me.cboTeam & "") = 0 Then
        Do something
    End If

  3. #3
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    another way is

    Code:
    If nz(Me.cboAssoc,"") = "" AND nz(Me.cboTeam,"") = "" Then
        'do something

  4. #4
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    More info

    In math, there is something called Precedence of Arithmetic Operators
    It codifies the order to do the calculations:
    Parentheses,
    Exponents,
    Multiplication/Division,
    Addition/Subtraction


    In coding, there is also something called the Precedence of Logical Operators
    It codifies the order to do the Logical order in programming:
    Not,
    And,
    Or




    So, when you write this:
    Code:
    If IsNull (Me!cboAssoc) Or Me!cboAssoc = "" AND IsNull (Me!cboTeam) Or Me!cboTeam Then
          Do Something
    End if
    VBA interprets it as
    Code:
    If IsNull (Me!cboAssoc) Or (Me!cboAssoc = "" AND IsNull (Me!cboTeam)) Or Me!cboTeam Then
          Do Something
    End if
    First it analyzes (Me!cboAssoc = "" AND IsNull (Me!cboTeam)), then that result is ORed with IsNull (Me!cboAssoc), then that result is ORed with Me!cboTeam

    So yo need to use Parentheses to control the logic order of the comparisons.
    Code:
    If (IsNull (Me!cboAssoc) Or Me!cboAssoc = "") AND (IsNull (Me!cboTeam) Or Me!cboTeam) Then   '<<-- this must evaluate to True for the next statement to execute
          Do Something
    End if

    Clear as mud??

Please reply to this thread with any new information or opinions.

Similar Threads

  1. IF Then Else VBA statement
    By ui7598h in forum Programming
    Replies: 7
    Last Post: 10-28-2015, 11:43 AM
  2. Replies: 11
    Last Post: 04-29-2015, 01:38 PM
  3. if statement in sql statement, query builder
    By 54.69.6d.20 in forum Access
    Replies: 4
    Last Post: 09-11-2012, 07:38 AM
  4. Replies: 7
    Last Post: 08-17-2011, 01:49 PM
  5. Need Help with SQL Statement
    By Hank44 in forum Programming
    Replies: 3
    Last Post: 11-08-2010, 05:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums