Results 1 to 7 of 7
  1. #1
    cwagenaar is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2015
    Location
    Canada
    Posts
    5

    Editing the Results of an Expression Concatenation

    Hi there,



    I am trying to concatenate the results of several fields into one field, with each result separated by a comma. However, some fields are empty, so I don't want there to be commas with nothing between (as I have to export the query as an Excel document, and then as a CSV). Here's an example of what I'm trying to do:

    Code:
    Expr 1: Iif(IsNull([Roles]![Expression]=True),"","Adminstrator, ") & Iif(IsNull([Roles]![Expr2]=True),"","Content Manager, ")
    That's just part of it, there are a couple more fields involved, and then I'll be doing a similar one for even more fields. Now, I've been trying to figure out a way to get rid of any unnecessary commas - I can't nest the Iif functions, because I may need the results of all of the fields, or just some of the fields, in this one expression. I could not find any expression functions that really met my needs, so I was attempting to write a new function in VBA, but I am failing miserably, having not touched Visual Basic in almost two years. It doesn't help, either, that if a person wants to use VBA in Access they have to define an Object and that is well outside of the realm of what I recall from VBA.

    I apologize if this thread is an ill fit for this forum, and should in fact be in the Programming forum. I've been at this for days and am really at a loss, so if anyone has any suggestions I would greatly appreciate them.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Looks like misplaced ) for the IsNull function.

    IIf(IsNull([Expression]), "", "Administrator, ") & IIf(IsNull([Expr2]), "", "Content Manager, ")

    Problem is will always have a comma at the end if there is at least one value.

    Don't always have to define an object in VBA.
    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.

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    Use a function. Paste this code into a module.
    then in your query the Concated field would be: ConCatFld: BuildFld([Adm], [Mngr],[CEO])


    Put all the fields needed as parameters to be checked:


    Code:
    public function BuildFld(pvAdm, pvMngr,pvCEO)
    dim vWord
    
    if not IsNull(pvAdm) then vWord = vWord & pvAdm & ","       '"Adminstrator"
    if not IsNull(pvMngr) then vWord = vWord & pvMngr & ","       ' "Content Manager, "
    if not IsNull(pvCEO) then vWord = vWord & pvCEO & ","       '"CEO, "
    
    
    
    
    'REMOVE LAST COMMA IF NEEDED
    vWord = left(vWord, len(vWord) - 2)
    
    
    BuildFld = vWord
    end function

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Probably need to include some code that first checks if the concatenation results in Null because will error if it is.
    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
    cwagenaar is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2015
    Location
    Canada
    Posts
    5
    Thanks, this seems to work really well! The only problem is that, when all of the fields return null, it seems to return commas. I'll share a screenshot:

    Click image for larger version. 

Name:	Capture.JPG 
Views:	7 
Size:	19.3 KB 
ID:	20743

    Any thoughts? If I can figure out how to get rid of those commas in the null fields, I'll be able to modify this piece of code to fit my needs for another expression, as well.

    Thanks again for all the help!

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    If you are getting commas then the variables are not actually Null, they have something, likely an empty string. Alternative code to handle that:

    If pvAdm & "" <> "" Then vWord = vWord & pvAdm & ", "
    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.

  7. #7
    cwagenaar is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2015
    Location
    Canada
    Posts
    5
    Quote Originally Posted by June7 View Post
    If you are getting commas then the variables are not actually Null, they have something, likely an empty string. Alternative code to handle that:

    If pvAdm & "" <> "" Then vWord = vWord & pvAdm & ", "
    Thank you! That solved it. I also had to use a variation of that for the end bit to trim any unnecessary commas/spaces.

    Code:
    If vWord <> "" Then vWord = Left(vWord, Len(vWord) - 2)

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

Similar Threads

  1. Replies: 2
    Last Post: 01-31-2015, 09:29 PM
  2. Replies: 5
    Last Post: 12-01-2014, 07:32 PM
  3. Replies: 3
    Last Post: 10-19-2013, 10:21 AM
  4. Expression gives wrong results
    By newtoAccess in forum Queries
    Replies: 22
    Last Post: 12-03-2010, 12:21 AM
  5. Replies: 0
    Last Post: 10-16-2008, 02:39 PM

Tags for this Thread

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