Merging data only if not null

Posted on 2004-11-25
Last Modified: 2012-06-27

im trying to merge data from 20 columns into one column with "and" or a comma as a separator. unfortunately, i keep on getting the null values as well.

ex. column 1 - null
     column 2 - null
     column 3 - gate

    merge into one column it looks like - "and and gate" or ", , gate"

how can i make access return the value only if it is not null - for twenty columns, i do not think it is practical to create if statements for all the possibilities. please help!!!!!


Question by:ankh04
    LVL 7

    Accepted Solution

    You will need to something on each column (except the first), but only once on each column (or write a function to do it and pass

    field1 + iif(isnull(field2),""," and "+field2) +  iif(isnull(field3),""," and "+field3) +  ect

    This will only add the 'and' or comma if the field you are adding has a value.

    LVL 33

    Expert Comment

    by:Mike Eghtebas
    if you want like:   ABC,n/a,CBS,n/a    use:

    Nz(Field1,"n/a") & ", " & Nz(Field2,"n/a") & ", " & Nz(Field3,"n/a") & ", " & Nz(Field4,"n/a")
    LVL 33

    Expert Comment

    by:Mike Eghtebas
    But, if you like ABC,CBS  and ignoring null values, then use

    Dim strTemp As String
    if Nz(Field1,"") <>"" Then strTemp =strTemp &Field1 & ", "
    if Nz(Field2,"") <>"" Then strTemp =strTemp &Field2 & ", "
    if Nz(Field3,"") <>"" Then strTemp =strTemp &Field3 & ", "
    if Nz(Field4,"") <>"" Then strTemp =strTemp &Field4 & ", "

    strTemp = Left(strTemp,Len(strTemp)-2)
    LVL 33

    Expert Comment

    by:Mike Eghtebas
    FYI: and empty field, depending in its field type could be Null or Empty.  Therefore we need to use following code to handle both:

    Nz(Field1,"") <>""

    Now, Field1 if it is and Empty string (""), Nz() will become "" and then will be compared with <>"".
    But, if it is a null value, it will change to "" thus covering both possiblities.

    Therefore, first code need to be:

    iif(Nz(Field1,"")="","n/a", Field1) & ", " & iif(Nz(Field2,"")="","n/a", Field2) & ", " & ...

    Also, you may consider using | (in my keyboard it is above Enter key).


    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

    Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
    I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
    In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

    733 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

    19 Experts available now in Live!

    Get 1:1 Help Now