Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

concatinate string with , whilst removing duplicate ,'s for null or empty fields

Posted on 2015-02-24
6
59 Views
Last Modified: 2015-07-08
Hi all

I'm wondering if its possible to concatenate a set of results with a "," but work it in such as was so that if one or more of the fields are empty/null I do not end up with multiple ,'s

I'm using addresses so for example rather than the following address showing as:
flat 4,, made up street,,,medway, kent, me1 23e

I would like it to be just single ,'s
flat 4, made up street, medway, kent, me1 23e
 
Regards
Neil
0
Comment
Question by:Neil Thompson
6 Comments
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 500 total points
ID: 40627842
Yes, you can use:

=[Field1] & (", " + [Field2]) & (", " + [Field3]) & (", " + [Field4]) & (", " + [Field5])

", " + Null returns Null.
The first field must have a value.

/gustav
0
 
LVL 30

Expert Comment

by:hnasr
ID: 40627864
Try this:

Type this code in a general module.

Public Function sqButl(strVar As String)
    Dim strVarl As String
    Dim finished As Boolean
    strVarl = strVar
    Do While Not (finished)
        strVar = Replace(strVar, ",,", ",")
        If strVarl = strVar Then
            finished = Not (finished)
            sqButl = strVar
        Else
            strVarl = strVar
        End If
    Loop
End Function

Open in new window

Try it from the immediate window. You can call it from your code or query.

? sqBut1 ("flat 4,, made up street,,,medway, kent, me1 23e"
Result:
flat 4, made up street,medway, kent, me1 23e

The code can be improved to adjust  space after comma.
For example
0
 
LVL 35

Expert Comment

by:PatHartman
ID: 40629450
Gus' solution is the simplest.  It works because it takes advantage of the different concatenation behavior exhibited by the & and the +.  Fields concatenated using & will substitute a ZLS if one of them is null whereas, fields concatenated using + will return null if one of the fields is null.

So:
"a" & "," = "a,"
but
null & "," = null

Just make sure to use parentheses to control the order of precedence to ensure that you get the result you want.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 3

Author Comment

by:Neil Thompson
ID: 40630293
Thanks all, I'm back in the office Friday so Ill test and award points then
0
 
LVL 3

Author Closing Comment

by:Neil Thompson
ID: 40872162
Excellent, thank you
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40872188
You are welcome!

/gustav
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question