Results 1 to 9 of 9
  1. #1
    Tlo is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jan 2016
    Posts
    41

    IIF Returning #error Message

    I'm new to Access and trying to get an IIF function to work in a query. I understand the concept as I've used them for years in Excel, but can't seem to get the syntax right because I keep getting a #error message in the field I'm trying to create. I need to evaluate what service was done on a vehicle and add the appropriate amount of time for Next Service Due field (field with the iif function). I know how to use DateAdd, so I've reduced the formula to this for testing purposes, and am still getting a #error message.



    Next Service Due: IIF([Service Category]= "650 Mile Service","OK","NOT OK")

    If the field Service Category is empty, NOT OK is returned. If it is populated with anything OK is returned.

    What am I missing?

    Thanks in advance for the help!

  2. #2
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I do not get any error messages. I get OK or NOT OK, even when Service Category is an empty field.

    What is the value of Service Category that causes you an error?
    What is the exact error message you are seeing?

  3. #3
    Tlo is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jan 2016
    Posts
    41
    Actually I just figured it out - Service Category field is a lookup field from another table, so Service Category was a number not a text When I did this it worked:

    Next Service Due: IIf([Service Category]=1,"No Further Service Required",IIf([Service Category]=2,DateAdd("m",6,[Service Date]),IIf([Service Category]=3,DateAdd("m",12,[Service Date]),IIf([Service Category]=4,DateAdd("m",24,[Service Date]),IIf([Service Category]=5,DateAdd("m",42,[Service Date]),IIf([Service Category]=6,DateAdd("m",72,[Service Date])))))))

    Thanks for quick reply though! I had been wrestling that one for a few hours now.

  4. #4
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    I bet you have set your Service Category field to be a lookup field in your table design. If so, think how much time you would have saved if you hadn't done that.

    Another tip to save time in the long run is to not have spaces in your field names - a) it is an extra 3+ characters to type each time, b) you forget and get errors and spend time trying to resolve, c) even with the square brackets, access will for no rational reason find the word 'date' (or other reserved words) upsetting and generate an ambiguous error when you least expect it

    Also, nested iifs can become difficult to manage so recommend you add the 'choose' and 'switch' functions to your repertoire

    your code would then be simplified to

    Code:
    Next Service Due: Choose([Service Category],"No Further Service Required",DateAdd("m",6,[Service Date]),DateAdd("m",12,[Service Date]), DateAdd("m",24,[Service Date]), DateAdd("m",42,[Service Date]),DateAdd("m",72,[Service Date]))

  5. #5
    Tlo is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jan 2016
    Posts
    41
    Wonderful - thanks for the advice Ajax.

  6. #6
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Why lookup fields (in tables) are evil: http://access.mvps.org/access/lookupfields.htm

  7. #7
    Tlo is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jan 2016
    Posts
    41
    Thanks for the link. I'm taking a course in Access beginning next week and am sure I will get a much better foundation for this. But how else would I set up a field in that table to only allow a certain list of values to be chosen?

  8. #8
    Tlo is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jan 2016
    Posts
    41
    Would it mean storing the results (and building the entry form) based off a query? Again I know this is a basic fundamental question, so forgive me for my ignorance. And thanks in advance for the help.

  9. #9
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    But how else would I set up a field in that table to only allow a certain list of values to be chosen?
    you do it in the form, not the table - the control in your form would have the same properties (rowsource, bound column, columncount and column widths) as you have currently set in the table

    Point is - when developing, you know you are working with a number and not text. and labels in forms can be anything you want

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

Similar Threads

  1. Replies: 5
    Last Post: 05-15-2015, 10:49 AM
  2. VBA Code Returning Error Run Time Error 3061
    By tgwacker in forum Access
    Replies: 2
    Last Post: 11-24-2013, 11:00 AM
  3. Replies: 15
    Last Post: 11-01-2013, 03:24 PM
  4. Replies: 7
    Last Post: 07-24-2013, 02:01 PM
  5. Replies: 2
    Last Post: 06-23-2012, 11:59 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