我在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
我想要这个结果(所有的人和所有规划者一起拥有相同的路线):
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
这可能与查询有关,还是仅通过VBA代码? 提前谢谢!
编辑@Dan(求助): 谢谢你的步骤,我有想法做这样的事情,但不知道具体如何。 下面的代码对我有用(它可以更简单,更少的代码,但它完成工作)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 & "')"
这给了我以下结果:
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
非常感谢!
答案 0 :(得分:0)
虽然Access SQL本身没有任何方法可以将字符串连接作为聚合组来执行,但您可以使用SQL和VBA的组合来实现此目的。
我不能给你实际的代码,但这个想法是按照以下几行:
答案 1 :(得分:0)
您必须创建VBA功能。然后使用以下查询。
<强> Here is the Source link 强>
SELECT CompanyName, ConcatRelated("Cust", "Table_name", "Route= " & [Route])
FROM Table_name;