Results 1 to 11 of 11
  1. #1
    DOHillier is offline Novice
    Windows 10 Access 2010 64bit
    Join Date
    Oct 2017
    Posts
    9

    IIF Statment to produce multiple values in a Query field

    Good morning,

    At my work we utilize a database that generates a series of reports on a daily basis. One of these reports lists the various projects/programs that our vessels are assigned to under the field name [Program]. Recently my boss, in an effort to track multi-tasked vessels, has requested that I add two extra fields, which I have called [SecondaryPgm] and [TertiaryPgm]. Due to the lack of available space on the sheet I am forced to look at combining these three fields into one.

    In the query that the report is based on, I created a new field and used the following IIF statment:

    IIf([secondarypgm]=Null,[program],IIf([TertiaryPgm]=Null,([program] & " / " & [SecondaryPgm]),([program] & " / " & [secondarypgm] & " / " & [Tertiarypgm])))



    The idea here is that if the secondary and tertiary program fields are not present (i.e. NULL) then it would only show the Primary program field. If there was a secondary program [but not a tertiary] it would show: [Program] & " / " & [SecondaryPgm] on the query.

    Finally if there was a tertiary program, then the instruction on the query would be: [program] & " / " & [secondarypgm] & " / " & [Tertiarypgm]

    My issue here is that the IIF statement I'm using seems to default to the 'False' result everytime which winds up looking like this: Program //

    Obviously this isn't really what I want to see. I've tried starting small, using the NZ function, and so far have come away fruitless. I have a strong feeling that it's probably a syntax thing but 5 hours at it is enough for now. Any idea what I'm doing wrong?

    Kindest Regards
    D

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Nothing is equal to Null I have seen mentioned.
    Use IsNull() https://learn.microsoft.com/en-us/of...snull-function
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    xps35's Avatar
    xps35 is online now Competent Performer
    Windows 10 Office 365
    Join Date
    Jun 2022
    Location
    Schiedam, NL
    Posts
    232
    Adding two extra fields is bad design. You need to create a new table for the related programs.
    Groeten,

    Peter

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    D,
    Can you show us the tables and relationships for the database on which you are creating reports? Adding extra fields does not seem appropriate for database (may work for spreadsheet).

  5. #5
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Agree with the others that you should have a join table to track the programs associated with each vessel).
    To do what you're describing in the first post you need to use "+" to concatenate the slash and the two programs that might be missing; + and Null will return Null:
    Program(s):[Program] & (" / " + [SecondaryPgm]) & (" / " + [TertiaryPgm])

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  6. #6
    DOHillier is offline Novice
    Windows 10 Access 2010 64bit
    Join Date
    Oct 2017
    Posts
    9
    First off, wow thanks for the quick replies,

    Second I think I have it. The new function that appears to work is this:

    IIf(IsNull([SecondaryPGM]) And IsNull([Tertiarypgm]),[program],IIf(IsNull([Tertiarypgm]), [program] & " / " & [secondarypgm], [program] & " / " & [secondarypgm] & " / " & [Tertiarypgm]))

    FWIW I knew going in that it was bad design practice but my boss isn't really a database kind of guy. Not that I don't appreciate your advice however. I do have a table for programs for this very reason.

    In response to Mr. Orange's question (and without going too much into it cuz I'm at work), the query gets its data from a table called [platformdaytoday]. The query pulls vessel/aircraft name [separate table], its location, programs assigned [yet another table], operational status, and commentary. The report pulls this data from the query where it's emailed out to stakeholders and senior management. This is how it's currently connected:



    Click image for larger version. 

Name:	Capture.JPG 
Views:	21 
Size:	42.7 KB 
ID:	49486

    I apologize for posting too early and taking up your time. Next time I'll put in another hour or so just to avoid this happening again :/

    Kindest Regards
    Dale

  7. #7
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    You should try my suggestion as well, looks a bit easier on the eye .

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    [
    I apologize for posting too early and taking up your time. Next time I'll put in another hour or so just to avoid this happening again :/]
    I think 5 hours is enough TBH

    You can also use Not IsNull ?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  9. #9
    DOHillier is offline Novice
    Windows 10 Access 2010 64bit
    Join Date
    Oct 2017
    Posts
    9
    Quote Originally Posted by Gicu View Post
    You should try my suggestion as well, looks a bit easier on the eye .

    Cheers,
    I'll give it a go and let you know how things go
    D

  10. #10
    DOHillier is offline Novice
    Windows 10 Access 2010 64bit
    Join Date
    Oct 2017
    Posts
    9
    Works perfect! Thanks again guys for your help. o/
    D

  11. #11
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    The same technique is usually used when assembling mailing addresses from various components that may or may not be populated.

    Good luck with your project!

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

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

Similar Threads

  1. Replies: 1
    Last Post: 10-23-2018, 03:21 AM
  2. Replies: 2
    Last Post: 05-17-2017, 06:17 AM
  3. Replies: 10
    Last Post: 03-05-2015, 07:33 PM
  4. Create a new field with iif statment in a query
    By justair07 in forum Queries
    Replies: 3
    Last Post: 02-05-2014, 12:36 PM
  5. Iif statment in query for multiple fields
    By Sayshelz in forum Queries
    Replies: 1
    Last Post: 09-18-2012, 01:36 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