IIF statement in query criteria based on checkbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Xenver
    New Member
    • Jul 2014
    • 20

    IIF statement in query criteria based on checkbox

    ok, so i have a form with unbound check boxes on it, i want to alter a query criteria based on the check boxes values,so i have this as an example of my query criteria:

    Code:
    IIf([Forms]![frm_Reports Credit]![SS 1]=True,>0,Null)
    SS 1 is the check box I'm trying to evaluate.>0 is what i want the query criteria to be if the box is checked, Null is the value i want the criteria to be if the box is unchecked.

    The statement i have now runs, but returns no results.
  • Seth Schrock
    Recognized Expert Specialist
    • Dec 2010
    • 2965

    #2
    I'm pretty sure that when I have done this that I included the whole criteria in the IIF statement. For example
    Code:
    WHERE IIF(Forms![frm_Reports Credit]![SS 1]=True
              , MyField > 0
              , MyField Is Null)

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32662

      #3
      Absolutely right Seth :-)

      IIf() is a function that returns a value. The value can be used within SQL code but it cannot return SQL code itself as such.

      Comment

      • twinnyfo
        Recognized Expert Moderator Specialist
        • Nov 2011
        • 3662

        #4
        However, wouldn't the result of the iif be in quotes, since it is being added to a SQL statement?

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32662

          #5
          No. The result would be a value certainly, but not necessarily a string.

          MyField > 0 and MyField Is Null are both expressions that return boolean values. As the WHERE clause in SQL actually works with boolean values - even though most of us are more used to seeing expressions that return boolean values - this code works fine.

          Comment

          • twinnyfo
            Recognized Expert Moderator Specialist
            • Nov 2011
            • 3662

            #6
            Aaaaahahhhh, I think I get it now. So, in theory, one could simply say

            Code:
            WHERE True
            And all records would be returned or

            Code:
            WHERE False
            And no records would be returned?

            Once again, I have learned something useful here on this forum. Thanks for your insight!

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32662

              #7
              Absolutely. That's a perfect understanding.

              That's also why one of my favourite techniques using the Select Case statement in VBA, which makes it way more flexible, is to say :
              Code:
              Select Case True
              Case X = 3
                  ...
              Case Y = "Bob"
                  ...
              End Select
              Last edited by NeoPa; Aug 18 '14, 02:25 AM.

              Comment

              • Xenver
                New Member
                • Jul 2014
                • 20

                #8
                Thanks Guys, you were all very helpful.

                Comment

                Working...