MS Access 2003 - 在表单上连接相同ID的字段类型

时间:2009-09-29 11:09:35

标签: ms-access vba concatenation

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

有一件事是,他为每个记录提供了重复项,并将不同的“类型”作为字段“标识符”(我称之为)...这是一个例子:

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 个答案:

答案 0 :(得分: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]);

答案 1 :(得分:2)

我找到了一个示例(here),它似乎正是您所寻找的:
Concatenate Column Values from Multiple Rows into a Single Column with Access

从以上链接:

  

问题

     

获得一个有意义的头衔   这篇文章是最难的部分。该   问题是我见过的一对   Access新闻组中的时间,但它   没有具体的描述很难描述   例。一个帖子到   comp.databases.ms-访问几年   以前这样说:

     

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

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

答案 2 :(得分: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

FROM:http://wiki.lessthandot.com/index.php/Concatenate_a_List_into_a_Single_Field_(Column)

答案 3 :(得分:0)

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