Limiting the values of a combo box using IIF in query criteria

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sandra Walsh
    New Member
    • Nov 2011
    • 62

    Limiting the values of a combo box using IIF in query criteria

    Hello -

    I am trying to limit the options that show in a combo box based on the value of another field.

    In the design view of my query, I have the following criteria in the ReferralRole_ID field:
    Code:
    =IIf([Form]![f_MainReferralForm]![ActivityType_ID]=9,(1 Or 2 Or 7),(3 Or 7))
    This translates to the following SQL:

    Code:
    SELECT t_ReferralRoles_LU.ReferralRole_ID, t_ReferralRoles_LU.ReferralRole
    FROM t_ReferralRoles_LU
    WHERE (((t_ReferralRoles_LU.ReferralRole_ID)=IIf([Form]![f_MainReferralForm]![ActivityType_ID]=9,((t_ReferralRoles_LU.ReferralRole_ID)=1 Or (t_ReferralRoles_LU.ReferralRole_ID)=2 Or (t_ReferralRoles_LU.ReferralRole_ID)=7),((t_ReferralRoles_LU.ReferralRole_ID)=3 Or (t_ReferralRoles_LU.ReferralRole_ID)=7))))
    ORDER BY t_ReferralRoles_LU.ReferralRole_ID;
    I am getting zero records in my combo.

    If I just put < 1 OR 2 OR 7 > in the criteria, it works fine.

    Not sure if the IIF statement is coded correctly.

    Many thanks
    Sandra
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    What it sounds like you're looking for is along the lines of this:

    If Activity Type is 9, then Referral Role ID can be 1, 2, or 7. Otherwise Referral Role ID can be 3 or 7. Is that correct?

    Comment

    • Sandra Walsh
      New Member
      • Nov 2011
      • 62

      #3
      Hello Rabbit,

      Yes, that is what I am trying to accomplish.

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32633

        #4
        Try for line #3 (The WHERE clause) :

        Code:
        WHERE (([Form]![f_MainReferralForm]![ActivityType_ID]=9) AND ([ReferralRole_ID] In(1,2,7))
           OR  ([Form]![f_MainReferralForm]![ActivityType_ID]<>9) AND ([ReferralRole_ID] In(3,7)))

        Comment

        • Sandra Walsh
          New Member
          • Nov 2011
          • 62

          #5
          Works Perfectly! I was not aware of the In(x,y, ...) option.

          Many thanks! :-)

          Comment

          • Sandra Walsh
            New Member
            • Nov 2011
            • 62

            #6
            PS - I presume IN = includes?

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32633

              #7
              Not exactly.
              Code:
              WHERE [A] In(X,Y,Z)
              This could be read out as :
              Where the value of [A] is found in the list of X,Y,Z.

              It can work with any type of value. Not just numeric but also Dates, Strings etc.

              Comment

              • Sandra Walsh
                New Member
                • Nov 2011
                • 62

                #8
                Thanks for the clarification - can think of lots of ways to use this!
                s-

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32633

                  #9
                  Indeed :-)

                  In() and Like can be very powerful constructs indeed.

                  Have fun.

                  Comment

                  Working...