Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ms access 一行中多个条目的串联_Ms Access_Vba - Fatal编程技术网

Ms access 一行中多个条目的串联

Ms access 一行中多个条目的串联,ms-access,vba,Ms Access,Vba,我正在尝试连接不同行中相同产品的属性列表 例如: 列A(水果名称)有水果名称,列B(水果颜色)有颜色。 我想要和水果同一排的水果颜色 |**fruit_name** | **fruit_colors** | |----------------|------------------| |Apple |Red | |Apple |Yellow | |Apple |Green

我正在尝试连接不同行中相同产品的属性列表

例如:
列A(水果名称)有水果名称,列B(水果颜色)有颜色。
我想要和水果同一排的水果颜色

|**fruit_name**  | **fruit_colors** |
|----------------|------------------|
|Apple           |Red               |
|Apple           |Yellow            |
|Apple           |Green             |
|Apple           |White             |
|Banana          |Red               |
|Banana          |Yellow            |
|Banana          |Green             |
|Banana          |White             |
|Plum            |White             |
|Plum            |Bluish            |
|Plum            |Purple            |
结果应该是:

|**name**        | **colors**                |
|----------------|---------------------------|
|Apple           | Red, Yellow, Green, White |
|Banana          | Red, Yellow, Green, White |
|Plum            | White, Bluish, Purple     |
这就是我所拥有的:

Set fruit\u name=rstsource.Fields(“水果”)
设置source\u fruit=rstsource.Fields(“水果列表类型”)
rstsource.MoveFirst
count=rstsource.RecordCount
计数器=0
水果名称=来源水果
结果=源\表
做
做
计数器=计数器+1
结果=结果&“,”和源_表
rstsource.MoveNext
循环直到计数器=计数或水果\u名称源\u水果
rstdest.AddNew
rstdest.Fields(“名称”)=水果名称
rstdest.字段(“颜色”)=结果
rstdest.Update
水果名称=来源水果
result=“”
循环直到rstsource.EOF
这就是结果——有些在前面有逗号

香蕉-白色,白色
苹果色、黄色、红色
香蕉红
香蕉-白色,白色
苹果,绿色
李子色,绿色
李子红
香蕉红

最后有一个

运行时错误3021


我会阅读并下载Allen Browne的Concat函数-它将完全满足您的需求


这将仅用于报告或显示目的-您不应该像这样存储数据。

如何创建此查询以合并值

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 

是否有一个查询可以实现这一点

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 **********
。。。并将“MyTable”替换为表的名称。如果“EventID” 数据类型不长,那么您需要在SQL语句中替换它, 也可以使用字段使用的任何数据类型


保存并运行查询。瞧!逗号分隔列表。

谢谢您的链接。我会读的。这将用于报告目的。我在Access中有几个问题。我创建了一个表,在该表中宏将在下一列中写入产品名称(我在上面的示例中使用了水果)和属性的concat。您从哪里获得source\u fruit的值?我做了一个声明,但没有包括在这里:Set source\u fruit=rstsource.Fields(“fruits\u list\u type”),我认为您的结果得到第一个值两次(result=source_table)。如果可以显示不正确的输出,则更容易发现问题。Hi@Stefan使用结果更新。
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 **********
SELECT EventID, fConcatFld("MyTable","EventID","PersonName","Long", EventID) 
AS PersonNames 
FROM MyTable 
GROUP BY EventID;