We help IT Professionals succeed at work.
Get Started

Concatenate mutilpe rows of data into a single field

Karen Schaefer
on
567 Views
Last Modified: 2013-11-28
I found the following code on ee
https://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_24977680.html?sfQueryTermInfo=1+10+30+concat+data

However, when I modify it for my needs it is returning the same value for every row.

I need to look at an WS_NO_UO and group together every instances of the MN into the same field.

Why is not moving to the next record of the APNo and returning its list of MN?

here is my code - KEEP IN MIND THAT I USE GLOBAL DECLARATIONS FOR MOST OF MY VARIABLES.

I also need to be able to handle empty values that will not return the following example:

, , , , , , ,  123435, 5435665, 24342,

Also hide the last comma.

Thanks
K
Public Function Concat(WSNO As String) As String

Dim strTopic As String
StrSQL = "SELECT APNo, WS_NO_UO, MN" & _
        " FROM Qry_PrgUODetail" & _
        " GROUP BY APNo, WS_NO_UO, MN" & _
        " ORDER BY WS_NO_UO"
     '   " WHERE WS_NO_UO = " & Chr(34) & gWSNO & Chr(34) & "" & _

Set rs = CurrentDb.OpenRecordset(StrSQL)
'Debug.Print StrSQL
    rs.MoveFirst

    Do Until rs.EOF
        strTopic = strTopic & rs![MN] & ", "
        rs.MoveNext
    Loop

    If Len(strTopic & "") > 0 Then
        strTopic = Left(strTopic, Len(strTopic) - 2)
    Else
        strTopic = ""
    End If

    Concat = strTopic

End Function

Open in new window

Comment
Watch Question
CERTIFIED EXPERT
Top Expert 2010
Commented:
This problem has been solved!
Unlock 1 Answer and 14 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE