Results 1 to 5 of 5
  1. #1
    dhicks19 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Apr 2012
    Posts
    17

    And or statement help

    Hi, please can you have a look at my code, I have this on a button on a form that has an ID number and two check boxed humidity and temp.


    My issue is that its triggering them all instead of just one and im getting multiple forms open.
    Im new to access and I dont know where I am going wrong,please help.


    Option Compare Database
    Option Explicit

    Private Sub Command2_Click()



    With DoCmd
    If Me.ID = "160" Or Me.ID = "161" Or Me.ID = "162" And Me.Humidity = True And Me.Temp = False Then

    .SetWarnings False
    .OpenQuery "qrymakecaldata"
    .OpenQuery "qrymakecaldata2"
    '.OpenQuery "qrymakecalref"
    .SetWarnings True
    .OpenForm "frmcalresultsmulti"
    .SelectObject acForm, "frmmakecaldata"
    .Minimize
    MsgBox ("TRUE FALSE ID")


    End If

    If Me.ID <> "160" Or Me.ID <> "161" Or Me.ID <> "162" And Me.Humidity = True And Me.Temp = False Then

    .SetWarnings False
    .OpenQuery "qrymakecaldata"
    .OpenQuery "qrymakecaldata2"
    '.OpenQuery "qrymakecalref"
    .SetWarnings True
    .OpenForm "frmcalresults2"
    .SelectObject acForm, "frmmakecaldata"
    .Minimize
    MsgBox ("TRUE FALSE NOT ID")



    End If



    If Me.ID = "160" Or Me.ID = "161" Or Me.ID = "162" And Me.Humidity = True And Me.Temp = True Then

    .SetWarnings False
    .OpenQuery "qrymakecaldata"
    .OpenQuery "qrymakecaldata2"
    .OpenQuery "qrymakecaldatatemp"
    '.OpenQuery "qrymakecalref"
    .SetWarnings True
    .OpenForm "frmcalresultsmulti"
    .SelectObject acForm, "frmmakecaldata"
    .Minimize
    MsgBox ("TRUE TRUE ID")

    End If

    If Me.ID <> "160" Or Me.ID <> "161" Or Me.ID <> "162" And Me.Humidity = True And Me.Temp = True Then



    .SetWarnings False
    .OpenQuery "qrymakecaldata"
    .OpenQuery "qrymakecaldata2"
    .OpenQuery "qrymakecaldatatemp"
    '.OpenQuery "qrymakecalref"
    .SetWarnings True
    .OpenForm "frmcalresults2"
    .SelectObject acForm, "frmmakecaldata"
    .Minimize
    MsgBox ("TRUE TRUE NOT ID")


    End If


    If Me.ID = "160" Or Me.ID = "161" Or Me.ID = "162" And Me.Temp = True And Me.Humidity = False Then


    .SetWarnings False
    .OpenQuery "qrymakecaldata"
    .OpenQuery "qrymakecaldatatemp"
    '.OpenQuery "qrymakecalref"
    .SetWarnings True
    .OpenForm "frmcalresultsmulti"
    .SelectObject acForm, "frmmakecaldata"
    .Minimize
    MsgBox ("TRUE FALSE ID")


    End If


    If Me.ID <> "160" Or Me.ID <> "161" Or Me.ID <> "162" And Me.Temp = True And Me.Humidity = False Then

    .SetWarnings False
    .OpenQuery "qrymakecaldata"
    .OpenQuery "qrymakecaldatatemp"
    '.OpenQuery "qrymakecalref"
    .SetWarnings True
    .OpenForm "frmcalresults2"
    .SelectObject acForm, "frmmakecaldata"
    .Minimize
    MsgBox ("TRUE FALSE NOT ID")


    End If





    If Me.ID = "160" Or Me.ID = "161" Or Me.ID = "162" And Me.Humidity = False And Me.Temp = False Then


    .SetWarnings False
    .OpenQuery "qrymakecaldata"
    .OpenQuery "qrymakecalref"
    .SetWarnings True
    .OpenForm "frmcalresultsmulti"
    .SelectObject acForm, "frmmakecaldata"
    .Minimize
    MsgBox ("FALSE ID")

    End If




    If Me.ID <> "160" Or Me.ID <> "161" Or Me.ID <> "162" And Me.Humidity = False And Me.Temp = False Then

    .SetWarnings False
    .OpenQuery "qrymakecaldata"
    '.OpenQuery "qrymakecalref"
    .SetWarnings True
    .OpenForm "frmcalresults"
    .SelectObject acForm, "frmmakecaldata"
    MsgBox ("FALSE NOT ID")
    .Minimize
    End If
    End With


    End Sub

  2. #2
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    The IF() statement need modified. The three Me.IDs need to be enclosed in parenthesis:

    Code:
    If (Me.ID = "160" Or Me.ID = "161" Or Me.ID = "162") And Me.Humidity = True And Me.Temp = False Then
    Do this for all of the IF()'s.

    This could probably be condensed by a rewrite since there are conditions that are similar.... If you want, I'll look at it tonight.

  3. #3
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    In addition to what ssanfu has pointed out, this:

    Me.ID <> "160" Or Me.ID <> "161" Or Me.ID <> "162" will ALWAYS be true, regardless of the value of Me.ID - if Me.ID is equal to one of those three values, then obviously it is not equal to the other two!

    If you want the condition to be true when Me.ID is not equal to any of those values, you can use

    (Me.ID <> "160" And Me.ID <> "161" And Me.ID <> "162" )
    or
    Not (Me.ID = "160" Or Me.ID = "161" Or Me.ID = "162" )

    In this case, I think the latter one is clearer.

    HTH

    John

  4. #4
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    This is how I would structure the code:
    Code:
    Private Sub Command2_Click()
       With DoCmd
          If (Me.ID = "160" Or Me.ID = "161" Or Me.ID = "162") Then
             If Me.Humidity = True Then
                If Me.Temp = True Then
                   '   I prefer to use the Execute Method
                   '               CurrentDb.Execute "qrymakecaldata", dbFailOnError
                   '               CurrentDb.Execute "qrymakecaldata2", dbFailOnError
                   '               CurrentDb.Execute "qrymakecaldatatemp", dbFailOnError
    
                   .SetWarnings False
                   .OpenQuery "qrymakecaldata"
                   .OpenQuery "qrymakecaldata2"
                   .OpenQuery "qrymakecaldatatemp"
                   '.OpenQuery "qrymakecalref"
                   .SetWarnings True
    
                   .OpenForm "frmcalresultsmulti"
                   .SelectObject acForm, "frmmakecaldata"
                   .Minimize
                   MsgBox ("TRUE TRUE ID")
                Else
                   '     Me.Temp = False
                   .SetWarnings False
                   .OpenQuery "qrymakecaldata"
                   .OpenQuery "qrymakecaldata2"
                   '.OpenQuery "qrymakecalref"
                   .SetWarnings True
                   .OpenForm "frmcalresultsmulti"
                   .SelectObject acForm, "frmmakecaldata"
                   .Minimize
                   MsgBox ("TRUE FALSE ID")
                End If   'Me.Temp
             Else
                '     If Me.Humidity = False
                If Me.Temp = True Then
                   .SetWarnings False
                   .OpenQuery "qrymakecaldata"
                   .OpenQuery "qrymakecaldatatemp"
                   '.OpenQuery "qrymakecalref"
                   .SetWarnings True
                   .OpenForm "frmcalresultsmulti"
                   .SelectObject acForm, "frmmakecaldata"
                   .Minimize
                   MsgBox ("FALSE TRUE ID")
                Else
                   '     Me.Temp = False
                   .SetWarnings False
                   .OpenQuery "qrymakecaldata"
                   .OpenQuery "qrymakecalref"
                   .SetWarnings True
                   .OpenForm "frmcalresultsmulti"
                   .SelectObject acForm, "frmmakecaldata"
                   .Minimize
                   MsgBox ("FALSE FALSE ID")
                End If  'Me.Temp
             End If  'Me.Humidity
          Else
             '         Not (Me.ID = "160" Or Me.ID = "161" Or Me.ID = "162")
             If Me.Humidity = True Then
                If Me.Temp = True Then
                   .SetWarnings False
                   .OpenQuery "qrymakecaldata"
                   .OpenQuery "qrymakecaldata2"
                   .OpenQuery "qrymakecaldatatemp"
                   '.OpenQuery "qrymakecalref"
                   .SetWarnings True
                   .OpenForm "frmcalresults2"
                   .SelectObject acForm, "frmmakecaldata"
                   .Minimize
                   MsgBox ("TRUE TRUE NOT ID")
                Else
                   '     Me.Temp = False
                   .SetWarnings False
                   .OpenQuery "qrymakecaldata"
                   .OpenQuery "qrymakecaldata2"
                   '.OpenQuery "qrymakecalref"
                   .SetWarnings True
                   .OpenForm "frmcalresults2"
                   .SelectObject acForm, "frmmakecaldata"
                   .Minimize
                   MsgBox ("TRUE FALSE NOT ID")
                End If  ' Me.Temp
             Else
                '     Me.Humidity = False
                If Me.Temp = True Then
                   .SetWarnings False
                   .OpenQuery "qrymakecaldata"
                   .OpenQuery "qrymakecaldatatemp"
                   '.OpenQuery "qrymakecalref"
                   .SetWarnings True
                   .OpenForm "frmcalresults2"
                   .SelectObject acForm, "frmmakecaldata"
                   .Minimize
                   MsgBox ("FALSE TRUE NOT ID")
                Else
                   '     Me.Temp = False
                   .SetWarnings False
                   .OpenQuery "qrymakecaldata"
                   '.OpenQuery "qrymakecalref"
                   .SetWarnings True
                   .OpenForm "frmcalresults"   '<< shouldn't this be "frmcalresults2"   ???
                   .SelectObject acForm, "frmmakecaldata"
                   .Minimize
                   MsgBox ("FALSE FALSE NOT ID")
                End If  'Me.Temp
             End If   'Me.Humidity
          End If   'Me.ID
       End With  'Docmd
    
    End Sub



    There is more that can be done to reduce duplication of commands. For example, these two lines are in every IF() block (8 places = 16 lines):
    Code:
    .SelectObject acForm, "frmmakecaldata"
    .Minimize
    They could all be deleted from the IF() blocks and moved to just above the "END SUB" line (1 place = 2 lines).

    Just my $0.02

  5. #5
    dhicks19 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Apr 2012
    Posts
    17
    thank you for your help

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

Similar Threads

  1. SQL statement
    By JohnPapa in forum Programming
    Replies: 1
    Last Post: 09-19-2011, 10:42 AM
  2. Replies: 7
    Last Post: 08-17-2011, 01:49 PM
  3. IIF statement
    By LilMissAttack in forum Queries
    Replies: 2
    Last Post: 08-05-2011, 08:49 AM
  4. iff Statement
    By tkandy in forum Access
    Replies: 0
    Last Post: 03-20-2011, 02:31 PM
  5. if statement
    By sirlosi in forum Queries
    Replies: 4
    Last Post: 03-07-2011, 11:26 AM

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