2

好的,所以工作中的一个人有一个小的访问数据库,他用来跟踪事情。他有他使用的这个表格,它已经查询了他需要什么并在表格上生成结果,而这正是他所需要的。

一件事是他对每条记录都有重复项,这些记录带有不同的“类型”作为字段“标识符”(我称之为)......这是一个例子:

ID     Name          Price     Type
1      Prodcut A     $10       A1
1      Product A     $10       A2
1      Product A     $10       A3
2      Product B     $12       A1
etc

自然这是应该发生的,他想查看所有类型,但考虑到它最终长达一英里,他问我是否有办法连接“类型”,因此将显示以下内容:

ID     Name          Price     Type
1      Prodcut A     $10       A1, A2, A3
1      Product B     $12       A1, A2, A3
1      Product C     $14       A1, A2, A3
2      Product D     $7        A1, A2, A3

...在表格上。谁能帮我解决这个问题?谢谢!

4

4 回答 4

3

好的,我找到了一个在VBA中创建的函数,可以在查询中使用它来检索表单的数据。

功能是

Public Function ConcatRelated(strField As String, _
    strTable As String, _
    Optional strWhere As String, _
    Optional strOrderBy As String, _
    Optional strSeparator = ", ") As Variant
On Error GoTo Err_Handler
    'Purpose:   Generate a concatenated string of related records.
    'Return:    String variant, or Null if no matches.
    'Arguments: strField = name of field to get results from and concatenate.
    '           strTable = name of a table or query.
    '           strWhere = WHERE clause to choose the right values.
    '           strOrderBy = ORDER BY clause, for sorting the values.
    '           strSeparator = characters to use between the concatenated values.
    'Notes:     1. Use square brackets around field/table names with spaces or odd characters.
    '           2. strField can be a Multi-valued field (A2007 and later), but strOrderBy cannot.
    '           3. Nulls are omitted, zero-length strings (ZLSs) are returned as ZLSs.
    '           4. Returning more than 255 characters to a recordset triggers this Access bug:
    '               http://allenbrowne.com/bug-16.html
    Dim rs As DAO.Recordset         'Related records
    Dim rsMV As DAO.Recordset       'Multi-valued field recordset
    Dim strSql As String            'SQL statement
    Dim strOut As String            'Output string to concatenate to.
    Dim lngLen As Long              'Length of string.
    Dim bIsMultiValue As Boolean    'Flag if strField is a multi-valued field.

    'Initialize to Null
    ConcatRelated = Null

    'Build SQL string, and get the records.
    strSql = "SELECT " & strField & " FROM " & strTable
    If strWhere <> vbNullString Then
        strSql = strSql & " WHERE " & strWhere
    End If
    If strOrderBy <> vbNullString Then
        strSql = strSql & " ORDER BY " & strOrderBy
    End If
    Set rs = DBEngine(0)(0).OpenRecordset(strSql, dbOpenDynaset)
    'Determine if the requested field is multi-valued (Type is above 100.)
    bIsMultiValue = (rs(0).Type > 100)

    'Loop through the matching records
    Do While Not rs.EOF
        If bIsMultiValue Then
            'For multi-valued field, loop through the values
            Set rsMV = rs(0).Value
            Do While Not rsMV.EOF
                If Not IsNull(rsMV(0)) Then
                    strOut = strOut & rsMV(0) & strSeparator
                End If
                rsMV.MoveNext
            Loop
            Set rsMV = Nothing
        ElseIf Not IsNull(rs(0)) Then
            strOut = strOut & rs(0) & strSeparator
        End If
        rs.MoveNext
    Loop
    rs.Close

    'Return the string without the trailing separator.
    lngLen = Len(strOut) - Len(strSeparator)
    If lngLen > 0 Then
        ConcatRelated = Left(strOut, lngLen)
    End If

Exit_Handler:
    'Clean up
    Set rsMV = Nothing
    Set rs = Nothing
    Exit Function

Err_Handler:
    MsgBox "Error " & Err.Number & ": " & Err.Description, vbExclamation, "ConcatRelated()"
    Resume Exit_Handler
End Function

并在查询中用作

SELECT Table1.ID, Table1.ProductName, Table1.ProductPrice, ConcatRelated("Type","Table1","ID = " & [Table1]![ID] & " AND ProductName = """ & [Table1]![ProductName] & """ AND ProductPrice = " & [Table1]![ProductPrice]) AS Expr1
FROM Table1
GROUP BY Table1.ID, Table1.ProductName, Table1.ProductPrice, ConcatRelated("Type","Table1","ID = " & [Table1]![ID] & " AND ProductName = """ & [Table1]![ProductName] & """ AND ProductPrice = " & [Table1]![ProductPrice]);
于 2009-09-29T12:14:36.870 回答
2

我找到了一个示例(此处),这似乎正是您正在寻找的内容:
Concatenate Column Values from Multiple Rows into a Single Column with Access

从上面的链接:

问题

为这篇文章起一个有意义的标题是最难的部分。这个问题是我在 Access 新闻组中看到过几次的问题,但是如果没有具体的例子就很难描述。几年前在 comp.databases.ms-access 上的一篇文章是这样说的:

我想将来自多个记录的字段值组合在一个字段中。例如:

Last     First     Code
-------  --------- ----
Lesand   Danny       1
Lesand   Danny       2
Lesand   Danny       3
Benedi   Eric        7
Benedi   Eric       14

结果应如下所示:

Last     First     Codes
-------  --------- -----
Lesand   Danny     1,2,3
Benedi   Eric       7,14
于 2009-09-29T11:46:56.740 回答
1

这些行中的某些内容可能适合,但连接通常不是一个好主意:

   Function ConcatList(strSQL As String, strDelim, _
                       ParamArray NameList() As Variant)
   ''Reference: Microsoft DAO x.x Object Library
   Dim db As Database
   Dim rs As DAO.Recordset
   Dim strList As String

   Set db = CurrentDb

   If strSQL <> "" Then
       Set rs = db.OpenRecordset(strSQL)

       Do While Not rs.EOF
           strList = strList & strDelim & rs.Fields(0)
           rs.MoveNext
       Loop

       strList = Mid(strList, Len(strDelim) + 1)
   Else

       strList = Join(NameList, strDelim)
   End If

   ConcatList = strList

   End Function

来自:http://wiki.lessthandot.com/index.php/Concatenate_a_List_into_a_Single_Field_(列)

于 2009-09-29T11:27:46.657 回答
0

为什么不尝试“交叉表查询”解决方案?

于 2009-09-29T12:52:39.027 回答