Results 1 to 8 of 8
  1. #1
    jyellis is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Posts
    20

    Help with a IIF statement in a Query

    Here is the scenario:

    In my UserID field:
    1. If it is not null then return the current value

    Then handle the null fields like this:
    2. If Username contains Loaner then UserID = Loaner
    3. If Username contains Mifi then UserID = Mifi
    4. If Username contains Alarm then UserId = Alarm

    5. All other blanks leave empty

    I start with 1777 the query works but returns only 1699. My formula is not returning the blank UserID records from step 5.



    Here is my formula:

    IIf([UserID] Is Not Null,[UserID],IIf([User Name] Like "*Loaner*","Loaner",IIf([User Name] Like "*Mifi*","MIFI",IIf([User Name] Like "*Alarm*","Alarm",IIf([User Name] Like "*Modem*","Modem","")))))


    Using Access 2016
    Thank you in advance

  2. #2
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    You don't have the right syntax in the first check for Null. Try:

    IIf(not isnull([UserID]) ,[UserID],IIf([User Name] Like "*Loaner*","Loaner",IIf([User Name] Like "*Mifi*","MIFI",IIf([User Name] Like "*Alarm*","Alarm",IIf([User Name] Like "*Modem*","Modem","")))))

  3. #3
    jyellis is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Posts
    20
    Thank you for the correct syntax:
    IIf(Not IsNull([UserID]),[UserID],IIf([User Name] Like "*Loaner*","Loaner",IIf([User Name] Like "*Mifi*","MIFI",IIf([User Name] Like "*Alarm*","Alarm",IIf([User Name] Like "*Modem*","Modem","")))))

    But it is still returning only the records where UserID is not blank. (1699 out 1777) I need all of them returned. I expected the problem with was the last part of my formula - "", but not sure.

  4. #4
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,936
    you have only provided part of the criteria - as written it could return anything but to what purpose? equal another field? true? false?

  5. #5
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Where are you using this IIf, i.e. in what part of the query? To get the information you want, it would have to be an expression in a query field.

  6. #6
    jyellis is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Posts
    20

    In the [UserId] field criteria row

    Quote Originally Posted by John_G View Post
    Where are you using this IIf, i.e. in what part of the query? To get the information you want, it would have to be an expression in a query field.

    Yes...In the [UserId] field criteria row

  7. #7
    jyellis is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Posts
    20
    If userid is blank and it meets criteria (loaner, mini, alarm) it returns - loaner, midi, alarm in the userid field

    the query is removing the 78 records that do not meet the criteria so instead of returning all 1777 records it only includes 1699 records
    in other words there should be 78 records where userid is null, but I want them included in the query

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    If you want all records, then why do you have this on Criteria row? You are excluding the 78 records because they don't meet the criteria. So don't put the expression on the Criteria row.

    Maybe you just want to construct a field with the calculation. Put the expression on the Field row. Here is example of alternative using Switch().
    Code:
    Category: Switch([User Name] Like "*Loaner*","Loaner", [User Name] Like "*Mifi*","MIFI", [User Name] Like "*Alarm*","Alarm", [User Name] Like "*Modem*","Modem", True,"None")
    Why is this data included in the [User Name] field? Username and this classifying value should be in separate fields to begin with.

    Advise not to use spaces in object and field names.
    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.

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

Similar Threads

  1. iif statement in a query
    By to47122 in forum Access
    Replies: 9
    Last Post: 09-16-2012, 07:32 PM
  2. if statement in sql statement, query builder
    By 54.69.6d.20 in forum Access
    Replies: 4
    Last Post: 09-11-2012, 07:38 AM
  3. Replies: 3
    Last Post: 07-10-2012, 05:23 AM
  4. Query/IiF statement
    By peacepower in forum Queries
    Replies: 1
    Last Post: 08-23-2011, 04:05 PM
  5. If then statement in query
    By ronnie4 in forum Queries
    Replies: 1
    Last Post: 01-20-2009, 10:49 AM

Tags for this Thread

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