Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combine data from multiple rows to a single record - part 2

Status
Not open for further replies.

oh704

Technical User
Oct 20, 2009
8
US
Hi All,

I have a similar problem to GreenLil with my data.
GreenLil, please excuse me for copying your sample data :).

Label ID First Name Last Name
10C000136 Bob Smith
10C000136 Jane Smith
10C000136 John Smith
10C000136 Bob Smith

I need to combine the unique first, last names into one record and achieve the following desired result.

Label ID Name
10C000136 Bob Smith & Jane Smith & John Smith

i don't believe this can be done on a sql, but perhaps something on vba and load the unique name into an array?

please advise.

Thanks so much.
 
Code:
Public Function concatRecords(lblID As Variant) As String
  Dim rs As DAO.Recordset
  Dim strSql As String
  
  strSql = "select * from tblData where [label ID] = '" & lblID & "'"
  Set rs = CurrentDb.OpenRecordset(strSql)
  
  Do While Not rs.EOF
    concatRecords = concatRecords & rs![First Name] & " " & rs![last Name] & "; "
    rs.MoveNext
  Loop
  If Not concatRecords = "" Then
    concatRecords = Left(concatRecords, Len(concatRecords) - 2)
  End If
End Function
Code:
SELECT DISTINCT 
 tblData.[Label ID], 
 concatRecords([label id]) AS [Names]
FROM 
 tblData;
There are some other FAQS as well
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top