Solved

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

Posted on 2015-02-24
6
54 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:NeilT
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 34

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 3

Author Comment

by:NeilT
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:NeilT
ID: 40872162
Excellent, thank you
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40872188
You are welcome!

/gustav
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now