【问题标题】:MS Access 2003 - Concatenating Field Types of Same ID on a FormMS Access 2003 - 在表单上连接相同 ID 的字段类型
【发布时间】:2010-12-02 06:58:48
【问题描述】:

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

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

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

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

【问题讨论】:

  • 在您的示例中,BC 都有 ID 1。对吗?
  • 哎呀,不应该是每个人的不同 ID 号......对不起!

标签: ms-access vba concatenation


【解决方案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_(Column)

【讨论】:

  • 是的,我理解,但是对于这个人需要它实际上是有道理的,因为他的需求不是很大,并且对于该事件代码的每个代码# 和描述,他需要查看每个“实例类型”可能适用....基本上就是这样。我知道串联就像关系回溯,但是...??
【解决方案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

【讨论】:

    【解决方案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]);
    

    【讨论】:

      【解决方案4】:

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

      【讨论】:

      • Crosstab 为每个值创建一列,而不是单个字段中的值的分隔列表。它还会生成可变数量的列(除非您巧妙地设计 SQL 以使用查找表作为列标题源)。
      • 我同意你的限制。这是一个“可能”的解决方案,具体取决于原始情况。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多