OpenForm WhereCondition 'Or' Syntax

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • iamstuart
    New Member
    • Mar 2010
    • 8

    OpenForm WhereCondition 'Or' Syntax

    Hi,
    I am using access 2003 and have some code that opens a form and filters it using the werecondition, the code works apart from the "Measure" i need it to filter it for any of the 3 options 'loft' Or 'boards' Or 'Loft & Boards' which filters fine when manually entered into the filter but am having problems finding the correct syntax for it to put this in automatically, the code i have currently which all works other than the "Measure =" is:
    Code:
    Dim stDocName As String
      
    stDocName = "Main Table"
    DoCmd.OpenForm stDocName, acNormal, , "[Postalcode] Like '" & PostalAreatxt & "*' and Measure= & 'loft' & " Or " & 'boards' & " Or " & 'Loft & Boards' and [Cancel Contract?] =False and IsNull([Completion Date]) and IsNull([Fit Scheduled Date1]) and IsNull([Fitters Names1])"
  • TheSmileyCoder
    Recognized Expert Moderator Top Contributor
    • Dec 2009
    • 2322

    #2
    Welcome to Bytes

    That is because each OR condition must contain its own True/false evaluation.
    Example of Bad:
    X=1 or 2 or 3
    Example of Good:
    X=1 or X=2 or X=3

    Using the _ will allow you to break up those long code lines to several lines to increase readability and maintain overview
    You also do not need & between single quotes '
    I have also added a set of parenthesis around your Or statements, as I presume they are meant as a group.
    Code:
    DoCmd.OpenForm stDocName, acNormal, , _
        "[Postalcode] Like '" & PostalAreatxt & "*' and (" & _
        "Measure= 'loft' Or " & _
        "Measure= 'boards' Or " & _
        "Measure= 'Loft & Boards') and " & _
        "[Cancel Contract?] =False and " & _
        "IsNull([Completion Date]) and " & _
        "IsNull([Fit Scheduled Date1]) and " & _
        "IsNull([Fitters Names1])"

    Finally to add some extra confusion you could also do:
    Code:
    Measure In ('loft','boards','Loft & Boards)
    (You might have to replace , with ; based on regional settings)

    Comment

    • iamstuart
      New Member
      • Mar 2010
      • 8

      #3
      This worked perfectly, thanks so much this had been frustrating me for days!

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32661

        #4
        As close to a perfect answer as you're likely to find anywhere.

        Nice one Smiley :)

        Comment

        Working...