View Full Version : Concatenate access field from multiple group sort


jb9
02-28-2009, 09:42 AM
I have an excel spreadsheet that sorts on brand, price, then color. I imported to access. 1 table, 1 report that group sorts brand and then price.

here's what my data look like:

brand....................price..................co lor
APPLE...................3.00...................whi te
APPLE...................3.00...................gre en
APPLE...................4.50...................pur ple
APPLE...................4.50...................ora nge
APPLE...................4.50...................yel low
TREE....................3.00...................whi te
TREE....................3.00...................gre en
TREE....................4.50...................pur ple
TREE....................4.50...................ora nge
TREE....................4.50...................yel low

I want to concatenate (color only) it like this:

brand....................price..................co lor
APPLE...................3.00...................whi te, green
APPLE...................4.50...................pur ple, orange, yellow
TREE....................3.00...................whi te, green
TREE....................4.50...................pur ple, orange, yellow

HiTechCoach
02-28-2009, 11:04 AM
If I were doing this, I woudl use the following.

Return a concatenated list of sub-record values (http://www.mvps.org/access/modules/mdl0004.htm)

jb9
02-28-2009, 12:51 PM
i actually have found something close to what i want to do.
i have been using this code, but it will either break the sort on either col 1 or col 2 depending on changing the code. I cant seem to get it to start on col1 and continue until col2 changes and combine all the colors into col3 to achieve what i show as the end result.

If Not rst.BOF And Not rst.EOF Then
rst.MoveFirst
strColumn1 = rst!Column1
strColumn2 = rst!Column2
strColumn3 = rst!Column3

rst.MoveNext
Do Until rst.EOF
If strColumn2 = rst!Column2 Then
strColumn3 = strColumn3 & ", " & rst!Column3
Else
sSQL = "INSERT INTO tblCopy (Column1, Column2, Column3) " _
& "VALUES('" & strColumn1 & "','" & strColumn2 & "','" & strColumn3 & "')"
db.Execute sSQL
strColumn1 = rst!Column1
strColumn2 = rst!Column2
strColumn3 = rst!Column3
End If
rst.MoveNext
Loop

lala
03-27-2009, 07:45 AM
i need the same thing done, but your example uses 2 tables
i only have one table that has multiple rows for each record, and i want to combine the rows based on one of the fields in the SAME TABLE

anyone?
thank you