我在访问vba脚本时非常初学,因为我正在学习宏。我一直在尝试连接不同行中相同产品的属性列表。例如,列A(fruit_name)具有水果名称,而列B(fruit_colors)具有颜色,但我希望水果颜色与水果在同一行。
|**fruit_name** | **fruit_colors** | <br>
|----------------|------------------|<br>
|Apple |Red |<br>
|Apple |Yellow |<br>
|Apple |Green |<br>
|Apple |White |<br>
|Banana |Red |<br>
|Banana |Yellow |<br>
|Banana |Green |<br>
|Banana |White |<br>
|Plum |White |<br>
|Plum |Bluish |<br>
|Plum |Purple |<br>
结果应为:
<br>
|**name** | **colors** | <br>
|----------------|------------------|<br>
|Apple | Red, Yellow, Green, White | <br>
|Banana | Red, Yellow, Green, White | <br>
|Plum | White, Bluish, Purple | <br>
这就是我所拥有的:
Set fruit_name = rstsource.Fields("fruits")
Set source_fruit = rstsource.Fields("fruits_list_type")
rstsource.MoveFirst
count = rstsource.RecordCount
counter = 0
fruit_name = source_fruit
result = source_table
Do
Do
counter = counter + 1
result = result & ", " & source_table
rstsource.MoveNext
Loop Until counter = count Or fruit_name <> source_fruit
rstdest.AddNew
rstdest.Fields("names") = fruit_name
rstdest.Fields("colors") = result
rstdest.Update
fruit_name = source_fruit
result = " "
Loop Until rstsource.EOF
编辑: 这是我得到的结果 - 有些人在前面有逗号。
香蕉 - 白色,白色
Apple - ,Yelow,Red
香蕉 - 红色
香蕉 - 白色,白色
Apple,绿色
梅花 - ,绿色
梅花 - ,红色
香蕉 - ,红色
最后还有一个运行时错误3021
任何反馈都将不胜感激。谢谢。
答案 0 :(得分:1)
我会阅读和下载Allen Browne的Concat函数http://allenbrowne.com/func-concat.html - 它会完全按照您的意愿执行。
这仅用于报告或显示目的 - 您不应该存储这样的数据。
答案 1 :(得分:0)
如何创建此查询以组合值?
I have a table with the following structure and values:
EventID PersonName
----------- ------------
1 John
1 Peter
1 Sylvia
2 John
2 Sylvia
3 Peter
3 June
I'd like to run a query and get results in the following format:
EventID PersonNames
-------- ---------------
1 John, Peter, Sylvia
2 John, Sylvia
3 Peter, June
是否有可以实现此目的的查询?
Concatenate fields in same table
Author(s) Dev Ashish
(Q) I need to concatenate a field in the format "Value1; Value2; Value3" etc. for each unique value of another field in the same table. How can I do this?
(A) Using the fConcatFld function, in the Northwind database, the following query should return a concatenated list of all CustomerIDs if you group by ContactTitle.
SELECT ContactTitle, fConcatFld("Customers","ContactTitle","CustomerID","string",[ContactTitle]) AS CustomersFROM CustomersGROUP BY ContactTitle;
'************ Code Start **********
'This code was originally written by Dev Ashish
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
'Code Courtesy of
'Dev Ashish
'
Function fConcatFld(stTable As String, _
stForFld As String, _
stFldToConcat As String, _
stForFldType As String, _
vForFldVal As Variant) _
As String
'Returns mutiple field values for each unique value
'of another field in a single table
'in a semi-colon separated format.
'
'Usage Examples:
' ?fConcatFld(("Customers","ContactTitle","CustomerID", _
' "string","Owner")
'Where Customers = The parent Table
' ContactTitle = The field whose values to use for lookups
' CustomerID = Field name to concatenate
' string = DataType of ContactTitle field
' Owner = Value on which to return concatenated CustomerID
'
Dim lodb As Database, lors As Recordset
Dim lovConcat As Variant, loCriteria As String
Dim loSQL As String
Const cQ = """"
On Error GoTo Err_fConcatFld
lovConcat = Null
Set lodb = CurrentDb
loSQL = "SELECT [" & stFldToConcat & "] FROM ["
loSQL = loSQL & stTable & "] WHERE "
Select Case stForFldType
Case "String":
loSQL = loSQL & "[" & stForFld & "] =" & cQ & vForFldVal & cQ
Case "Long", "Integer", "Double": 'AutoNumber is Type Long
loSQL = loSQL & "[" & stForFld & "] = " & vForFldVal
Case Else
GoTo Err_fConcatFld
End Select
Set lors = lodb.OpenRecordset(loSQL, dbOpenSnapshot)
'Are we sure that duplicates exist in stFldToConcat
With lors
If .RecordCount <> 0 Then
'start concatenating records
Do While Not .EOF
lovConcat = lovConcat & lors(stFldToConcat) & "; "
.MoveNext
Loop
Else
GoTo Exit_fConcatFld
End If
End With
'That's it... you should have a concatenated string now
'Just Trim the trailing ;
fConcatFld = Left(lovConcat, Len(lovConcat) - 2)
Exit_fConcatFld:
Set lors = Nothing: Set lodb = Nothing
Exit Function
Err_fConcatFld:
MsgBox "Error#: " & Err.Number & vbCrLf & Err.Description
Resume Exit_fConcatFld
End Function
'************ Code End **********
将fConcatFld()函数复制并粘贴到代码模块中。改变 遵循VBA代码行:
lovConcat = lovConcat&amp; lors(stFldToConcat)&amp; “;”
到:
lovConcat = lovConcat&amp; lors(stFldToConcat)&amp; “,”
...然后保存并编译代码。
接下来,创建一个新查询并在SQL View中打开查询并粘贴 将SQL语句跟随到SQL视图窗格中:
SELECT EventID, fConcatFld("MyTable","EventID","PersonName","Long", EventID)
AS PersonNames
FROM MyTable
GROUP BY EventID;
...并将“MyTable”替换为您的表名。如果是“EventID” 数据类型不长,那么你需要在SQL语句中替换它, 您的字段使用的数据类型也是如此。
保存并运行查询。瞧!以逗号分隔的列表。