Results 1 to 7 of 7
  1. #1
    IroncladRooster is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Sep 2014
    Posts
    4

    Conditional query concatenation

    I am brand new to access and am writing my first query.

    My first column concatenates first name middle initial (if available) and last name. I am using the below expression to return that result.

    FullName: [Name_Only]![First Name] & (" "+[Name_Only]![MI]) & (" " & [Name_Only]![Last Name])
    (with Name_Only being the name of my table)

    That works flawlessly.

    In trying to build my next expression I tried to follow the pattern from the expression above but it is not working for me. I am sure I am misunderstanding the basics behind the concatenation process.

    What I am trying to express next is a concatenation of multi part job titles. In my table I have a have a field titles "title 1", "title 2", and "title 3". Some people in our organization are just "vice presidents' so they only need one title, but others are Vice President, Managing Director, President of awesomeness. In that case he / she would have Title 1, 2 and 3. I will always need a result from Title 1, but I only need a title 2 if 1 is present and will only need 3 if both 1 and 2 are present.

    My concatenation attempt looks like this



    FullTitle: [Title, prior exp]![Title 1] & (", "+[Title, prior exp]![Title 2]) & (", "+[Title, prior exp]![Title 3])
    (with Title, prior exp being the name of my table)

    However this spits out Title 1, Title 2, Title 3 and keeps the commas and extra spaces in even when there is no second or third title. It seems like the + operator isn't picking up the null value and is still returning the ", " by itself.
    I have results that look like this (note the red commas at the end).
    Vice President, Managing Director, President of awesomeness
    Vice President, ,
    Managing Director, President of Semi-awesomeness,

    I'm sure there is a pretty simple fix. Thanks for your help. I look forward to bugging you guys with lots of other equally simple questions in the days and months ahead

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I would suggest renaming your tables. Names should only be letters, numbers and the underscore. NO spaces or punctuation.

    [Title, prior exp] <= no commas

  3. #3
    IroncladRooster is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Sep 2014
    Posts
    4
    Quote Originally Posted by ssanfu View Post
    I would suggest renaming your tables. Names should only be letters, numbers and the underscore. NO spaces or punctuation.

    [Title, prior exp] <= no commas
    Thanks for the suggestion. I tried that, but it didn't fix the problem.

    I ended up going with a long If function to solve the problem. I'm all ears to know if there is a better or more efficient way of doing this, but this is what I have so far.

    FullTitle: [titlepriorexp]![Title 1] & IIf([titlepriorexp]![Title 2]="","",", " & [titlepriorexp]![Title 2]) & IIf([titlepriorexp]![Title 3]="","",", " & [titlepriorexp]![Title 3])

    *note - I renamed my table "titlepriorexp"

  4. #4
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    I think you should be checking for Null instead of the empty string (""). An empty string is not the same as Null, so
    the expression [titlepriorexp]![Title 2]="" is False when [titlepriorexp]![Title 2] is Null.

    Using the Nz function, try something like this:

    [titlepriorexp]![Title 1] & Nz([titlepriorexp]![Title 2],"", ", " & [titlepriorexp]![Title 2]) & Nz([titlepriorexp]![Title 3],"", ", " & [titlepriorexp]![Title 3])

    John

  5. #5
    IroncladRooster is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Sep 2014
    Posts
    4
    Quote Originally Posted by John_G View Post
    Hi -

    I think you should be checking for Null instead of the empty string (""). An empty string is not the same as Null, so
    the expression [titlepriorexp]![Title 2]="" is False when [titlepriorexp]![Title 2] is Null.

    Using the Nz function, try something like this:

    [titlepriorexp]![Title 1] & Nz([titlepriorexp]![Title 2],"", ", " & [titlepriorexp]![Title 2]) & Nz([titlepriorexp]![Title 3],"", ", " & [titlepriorexp]![Title 3])

    John
    Thanks for the tip John. I just plugged your formula in but got the below error:
    "Wrong number of arguments used with function in query expression <and then it lists the formula you built>".

    I've been trying to debug it, but haven't had any luck yet. Any ideas?

  6. #6
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Oops!

    Sorry - yes, I messed that up. Too early on a Monday and not enough coffee yet.

    You do need the Iif - try it like this:

    FullTitle: [titlepriorexp]![Title 1] & IIf(isnull([titlepriorexp]![Title 2]),"",", " & [titlepriorexp]![Title 2]) & IIf(isnull([titlepriorexp]![Title 3]),"",", " & [titlepriorexp]![Title 3])

    Sorry 'bout that!

    John

  7. #7
    IroncladRooster is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Sep 2014
    Posts
    4
    Quote Originally Posted by John_G View Post
    Oops!

    Sorry - yes, I messed that up. Too early on a Monday and not enough coffee yet.

    You do need the Iif - try it like this:

    FullTitle: [titlepriorexp]![Title 1] & IIf(isnull([titlepriorexp]![Title 2]),"",", " & [titlepriorexp]![Title 2]) & IIf(isnull([titlepriorexp]![Title 3]),"",", " & [titlepriorexp]![Title 3])

    Sorry 'bout that!

    John
    Thanks for your help John. Works great.

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

Similar Threads

  1. Concatenation
    By cbende2 in forum Access
    Replies: 14
    Last Post: 08-01-2014, 01:32 PM
  2. Concatenation
    By lantoni in forum Access
    Replies: 31
    Last Post: 03-06-2014, 07:29 AM
  3. Replies: 6
    Last Post: 05-26-2013, 09:10 PM
  4. Concatenation
    By Rhubie in forum Access
    Replies: 3
    Last Post: 08-30-2012, 05:09 PM
  5. Query and Concatenation
    By Try2Live4God in forum Programming
    Replies: 2
    Last Post: 05-25-2010, 03:45 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