Results 1 to 14 of 14
  1. #1
    yes sir is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Aug 2010
    Posts
    159

    Using concatenations in Access

    I am trying to get a combination of four different fields to display correctly when one or multiple fields do not have a value.

    The field names are:
    City
    State
    High School
    Prep School


    So when all fields have a value it will look as so: City, State/ High School/ Prep School. However, when a field is empty I want the punctuation before the value to not show.

    Example: If City field doesn't have a value I want it to show as: State/ High School/ Prep School. Not as: ,State/ High School/ Prep School.

    Example: If City and State don't have values then I want it to show: High School/ Prep School. Not as: /High School/ Prep School.



    Example: If City, State, and High School are empty then I want it to show as: Prep School. Not as: /Prep School.


    If anyone could help or simply just give me a link so I can learn how to do this on my own. This is probably easy stuff for most of you, but I have had help before on this but still don't quite understand how to write up the expression. I know you use the IIf, but I don't really understand what it means.

    Thanks

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,917
    Actually if you use the "+" instead of the normal Access string concatenation character of "&" then the null fields will not show. Does that give you some ideas?

  3. #3
    NoellaG's Avatar
    NoellaG is offline VIP
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,180
    HI,

    if you want to use the iif (immediate if) function, the syntax is:
    Iif(test,value if true, value if false)
    So, in your case the test can be :
    if the field contains a value (lenght > 0),
    the value if true is :
    the field contant + "/",
    if the length is not > 0 the value is :
    an empty string,
    and the complete expression could be:
    Iif(len([MyFieldname]) >0,[MyFieldName] & "/","")
    gr
    NG

  4. #4
    yes sir is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Aug 2010
    Posts
    159
    Here is what I ended up using:

    =IIf(IsNull([City]) Or [City]="","",("" & [City] & ", "))+IIf(IsNull([State]) Or [State]="","",("" & [State]))+IIf(IsNull([High School]) Or [High School]="","",("/ " & [High School])) & IIf(IsNull([Prep School]) Or [Prep School]="","",("/ " & [Prep School]))

    However, using this expression, if a user only enters the City, HS, and Prep School. Then it would look like this: City, / High School/ Prep School. However, I assume, that if the user is entering the City, then more than likely they would enter the State for that city, so I doubt this would be a problem. If anyone knows another way I could go about this to where this is avoided please share.

    Thanks for your help

  5. #5
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,917
    If you would, try this expression for me please:
    ([City] + ", ") & ([State] + "/ ") & ([High School] + "/ ") & [Prep School]

  6. #6
    yes sir is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Aug 2010
    Posts
    159
    Quote Originally Posted by RuralGuy View Post
    If you would, try this expression for me please:
    ([City] + ", ") & ([State] + "/ ") & ([High School] + "/ ") & [Prep School]
    That works just the same. Thank you sir.

  7. #7
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,917
    You're welcome and thanks for giving it a try.

  8. #8
    yes sir is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Aug 2010
    Posts
    159
    Just realized what I was using wasn't working correctly, but this expression is on the money: =([City]+", ") & ([State]) & "/ "+([High School]) & "/ "+([Prep School])

  9. #9
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,917
    Excellent! Glad to hear you got it sorted.

  10. #10
    TheShabz is offline Court Jester
    Windows XP Access 2003
    Join Date
    Feb 2010
    Posts
    1,368
    just to add, be really careful about using + to concatenate. if you try to concatenate two numbers with a + you'll just add them together instead. I try to avoid using + to concatenate and just use the nz() function to take care of nulls.

  11. #11
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,917
    "+" propagates Nulls and "&" does not. It can be useful sometimes as in the example I provided in Post #5.

  12. #12
    TheShabz is offline Court Jester
    Windows XP Access 2003
    Join Date
    Feb 2010
    Posts
    1,368
    I agree that it has its benefits, just wanted to point out a caveat.

  13. #13
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    I'm with Allan. I've always loved the simplicity of using + in these types of situations. These apparently return the same thing; one is about 4 times longer than the other:

    Code:
    =IIf(IsNull([City]) Or [City]="","",("" & [City] & ",  "))+IIf(IsNull([State]) Or [State]="","",("" &  [State]))+IIf(IsNull([High School]) Or [High School]="","",("/ " &  [High School])) & IIf(IsNull([Prep School]) Or [Prep  School]="","",("/ " & [Prep School]))
    =([City]+", ") & ([State]) & "/ "+([High School]) & "/ "+([Prep School])
    I'm lazy; I choose the short one.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  14. #14
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,917
    And I'm more lazy than Paul and can't type either so you know from where I come.

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