Results 1 to 9 of 9
  1. #1
    TheHarleygirl2005 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2013
    Posts
    34

    IIf in Query


    I have a database where my "CourseID" field lists specific training courses. "Other" is one of the choices. If you select "Other" you are required to enter a course name in the "CourseDescription" field. I have a lot of "Other" listings in my query. What I would like to do is if the CourseID is "Other", leave it blank in my query otherwise list the name of the course. Would I do this with an IIF statement? and if so, would it go in the criteria section of the query? I have never used the IIF function before and am unsure how to make this work.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    It would go as a new field, not in the criteria. Something like:

    IIf(CourseID = "Other", "", CourseName)

    Access will alias the new field as Expr1, which you can change to whatever you want.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    TheHarleygirl2005 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2013
    Posts
    34
    I am getting an error (see attached)

    Click image for larger version. 

Name:	Error Msg.jpg 
Views:	9 
Size:	178.5 KB 
ID:	17172

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,602
    Don't see any aggregate function calculations. Why using Totals query?

    Don't see [CourseName] field in the tables. Is it actually in TrainingT?

    Is CourseID a number type field? Do you have lookup alias on this field in table? Advise not to do that, review http://access.mvps.org/access/lookupfields.htm
    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
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    Try changing Group By to Expression. It looks like CourseName needs to be CourseDescription? Does CourseID contain the text "Other", or is it perhaps a numeric ID field?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    I'll get out of the way.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    TheHarleygirl2005 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2013
    Posts
    34
    Okay, I figured it out. CourseID is pulling from a table of Course Names. I changed the CourseID to a number rather than "Other" and it worked. Thank you so much for your help!

  8. #8
    TheHarleygirl2005 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2013
    Posts
    34
    The totals query is calculating number of hours. I have a start time and end time and need to calculate totals hours for that course.

    I do have one question about your answer. I have about twenty to thirty courses each year, so I have a table that lists course names and then use lookup in the table for the CourseID field. You recommend not doing that, how would I accomplish this? The Course Name Table only has two fields Course Name and an auto generated number for the key. The only other way I can think of doing this is to add both tables to a query and then use a list box or combo box on the form to pull the information.

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,602
    I don't build lookups in tables. I use combo or list boxes on form.

    You are saving a course ID and name into TrainingT? I would not have an "Other" course record. I would save only the ID or the name. If saving the ID, would have method for creation of new record into Courses table. This is what the NotInList event of list and combo boxes is for.

    Your image does not show linking on PK field of EmployeeT. PK should be saved as FK. Either change the PK to MIS or save MemberID as FK. MemberID and MIS should not both be in TrainingT.

    Also, advise no spaces or special characters/punctuation (underscore is exception) in naming convention. Better would be CarNum, Car_Num, CarNo.
    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.

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