Results 1 to 11 of 11
  1. #1
    james28 is offline Advanced Beginner
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2014
    Posts
    92

    Query criteria using IIF when false

    Hello all!




    I am very new to Access and have managed to run into a road block of sorts. I have a very simple query created that pulls from a table in an access 2010 database. I then have an equally simple form named "testform" that has a single combobox named "ComboAnimalType" and a run query button. The combobox has 3 choices, which were manually entered. Dog, Cat and All. In my table, I have a field called Animal, which lists 10 different types of animals, 2 of which are Dog and Cat.


    For now, I want "Cat" and "All" to be treated the same way. If either of the options are selected, I want it to be as if there is nothing in the query's criteria for that field. Put another way, I want it to display everything.

    When I select "Dog", I want the query to be limited based on the criteria that animal = Dog.


    Using an IIF expression, I am able to accomplish the Dog portion. The problem that I am running in to is that I can not seem to figure out how to make the false portion of the IIF display all. I have tried the following, all of which work when the combobox is set to Dog, but return nothing when the combobox is set to Cat or All.

    IIf([forms]![testform]![ComboAnimalType]="Dog","Dog","*")


    IIf([forms]![testform]![ComboAnimalType]="Dog","Dog",)


    IIf([forms]![testform]![ComboAnimalType]="Dog","Dog","")


    IIf([forms]![testform]![ComboAnimalType]="Dog","Dog",<>null)


    IIf([forms]![testform]![ComboAnimalType]="Dog","Dog",<>"null")


    IIf([forms]![testform]![ComboAnimalType]="Dog","Dog",Like "*")


    IIf([forms]![testform]![ComboAnimalType]="Dog","Dog",*)



    Just to check to see if I had the IIF completely wrong, I changed it to
    IIf([forms]![testform]![ComboAnimalType]="Dog","Dog","Cat")

    This works just fine to limit the query based on animal being Cat, if anything other than Dog is selected in my combobox, so I know that the expression is in the right order, I am just puzzled as to what I need to use in order to show all results when false.



    If you could provide any assistance, I would greatly appreciate it.


    Thank you!
    James

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,640
    Try:

    WHERE [Type] LIKE IIf([forms]![testform]![ComboAnimalType]="Dog", "Dog", "*")

    But why not:

    WHERE [Type] LIKE IIf([forms]![testform]![ComboAnimalType]="All", "*", [forms]![testform]![ComboAnimalType])
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    james28 is offline Advanced Beginner
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2014
    Posts
    92
    June,

    Thank you for the prompt reply. I really appreciate it.


    Using the value of the combo box was my end goal, I was just trying to simplify as much as possible, to focus on the part that is giving me trouble.


    I was not able to get your example to work, but I think that is because I do not fully understand it.


    I am currently working in Design view on the query. I had exactly what I had pasted in my original reply in the Criteria section of the Animal column of my query.

    When I tried the same thing with your example, leaving out the WHERE [Type] LIKE, it does not work.

    I then tried to put your exact line in the criteria space in design view and after I saved and closed the query, when I opened it again, it created a new column called "WHERE [Type]"

    The "WHERE [Type]" column was unchecked and in is criteria section it had Like IIf([forms]![testform]![ComboAnimalType]="Dog","Dog","*")

    When I used the form to generate the query, selecting Dog returned 0 results (when it should have returned 2). Selecting anything other than Dog, like Cat or All, returned all of the results as it should have.


    If I did something wrong, could you please point me in the right direction?

    Thank you again!

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,640
    Is the combobox a lookup? Is it multi-column? Is the actual value of the combobox the text descriptor or is it a number ID? Is the Type field a number or text type?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    james28 is offline Advanced Beginner
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2014
    Posts
    92
    June,

    Thank you again for the quick reply. I will try to answer as best I can.

    Is the combobox a lookup? No, I do not think so. I am entering the values manually.

    Is it multi-column? No, I do not think so. There is only 1 answer showing in the combobox at a time.

    Is the actul value of the combobox the text descriptor or is it a number ID? I believe that it is the actual text, not a number ID

    Is the Type field a number or a text type? That depends, I have no "type" field. If you mean the "Animal" from my table, the field that contains the type of animal, then yes. I have that set to Text.

    I have attached a copy of what I am working with. That may make it easier.

    Database114.accdb


    Thank you again for the help!
    James

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,640
    I was only guessing when I used field name Type. Use your actual field name, which I see now is Animal and when I re-read your original post, I see it there as well. I had missed that detail.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    james28 is offline Advanced Beginner
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2014
    Posts
    92
    June,

    Blah! I should have realized that. I was wondering what it meant, but it sounded official.


    I updated my query to include Animal instead of Type. I deleted the criteria completely, so that I could make sure not to miss anything. When I did so, I did the same behavior of creating a new column. This time, the column was named "WHERE [Animal]".

    After saving and closing the query, I went back to the form to run it and it still works for "Cat" and "All", but returns 0 results for "Dog".



    Is there anything that I am missing?


    Thank you!

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,640
    Open the query in design view and put the criteria under the Animal field but don't include "WHERE Animal", just the LIKE IIf expression.

    What I originally posted was the syntax as viewed in the SQL window.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    james28 is offline Advanced Beginner
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2014
    Posts
    92
    June,

    Thank you, thank you, thank you!

    That seems to have done it!

    I guess I was very close before, I was just missing the Like in the front. If you do not mind my asking, why is Like required before the IIF? IIF by itself allows the true condition to work, but not the false, which seems strange.


    Thank you again!
    James

  10. #10
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,640
    Without LIKE, the query will assume = operator. LIKE is needed with wildcard.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    james28 is offline Advanced Beginner
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2014
    Posts
    92
    June,


    That makes sense. Thank you again for taking the time to help. I have marked the thread as solved.

    -James

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

Similar Threads

  1. Replies: 5
    Last Post: 09-20-2012, 03:27 PM
  2. Replies: 5
    Last Post: 08-02-2012, 09:44 AM
  3. SELECT "False" if any records are false?
    By Azurewrath in forum Queries
    Replies: 6
    Last Post: 12-21-2011, 03:36 PM
  4. Replies: 1
    Last Post: 07-13-2011, 11:00 AM
  5. FilterOn = False
    By ybg1 in forum Forms
    Replies: 1
    Last Post: 06-21-2011, 01:23 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