I have following table in Access 2007:
Route Cust Planners
4401 1004 jasper
4401 1005 onno
4401 1006 jasper
4402 1007 bart
4402 1008 marcel
4402 1059 onno
4403 1124 bart
4403 1165 marcel
4403 1198 marcel
4403 1201 mustafa
4403 1225 bart
4401 1178 maurice
And I want this result (all the cust together and all the planners together that have same route):
Route Cust Planners
4401 1004;1006;1178;1005 jasper;maurice;onno
4402 1007;1008;1059 bart;marcel;onno
4403 1124;1225;1165;1198;1201 mustafa;marcel;bart
Is this possible to do with a query or only via VBA-code? Thanks in advance!
EDIT @Dan (SOLVED): Thanks for the steps, I had the idea of doing something like this but didn't know exactly how. Below the code that worked for me (It can maybe be done easier and with less code but it does the job)
Dim rs As DAO.Recordset
Dim cust, planners As String
Dim route, route2, cntr As Integer
DoCmd.RunSQL "Delete * FROM details2"
Set rs = CurrentDb.OpenRecordset("select * from details order by route, cust")
rs.MoveFirst
cntr = 0
Do While Not rs.EOF
route = rs.Fields("Route")
'If route readed from recordset is not the same as local variable then insert that route and give local variable the new route and clear cust and planners
If route <> route2 Then
'If going through loop for first time => do no insert
If cntr > 0 Then
'Delete last comma for both cust and planners field and insert that route
cust = Left(cust, Len(cust) - 1)
planners = Left(planners, Len(planners) - 1)
DoCmd.RunSQL "insert into details2 (Route, Cust, Planners) Values (" & route2 & ", '" & cust & "', '" & planners & "')"
End If
route2 = route
cust = ""
planners = ""
End If
'If route readed is the same as local variable, add cust and planner to the string but only if it isn't in the string already
If route = route2 Then
If InStr(cust, CStr(rs.Fields("cust"))) = 0 Then cust = cust + CStr(rs.Fields("cust")) & ","
If InStr(planners, rs.Fields("planners")) = 0 Then planners = planners + rs.Fields("planners") & ","
End If
rs.MoveNext
cntr = cntr + 1
Loop
'Delete last comma for cust and planners field and add last route
cust = Left(cust, Len(cust) - 1)
planners = Left(planners, Len(planners) - 1)
DoCmd.RunSQL "insert into details2 (Route, Cust, Planners) Values (" & route2 & ", '" & cust & "', '" & planners & "')"
And this gave me following results:
Route Cust Planners
4401 1004,1005,1006,1178 jasper,onno,maurice
4402 1007,1008,1059 bart,marcel,onno
4403 1124,1165,1198,1201,1225 bart,marcel,mustafa
Thank you very much!