簡體   English   中英

MS Access 2003-在表單上串聯相同ID的字段類型

[英]MS Access 2003 - Concatenating Field Types of Same ID on a Form

好的,一個在工作的人有一個訪問數據庫,他可以用來跟蹤事物。 他使用的表格已經查詢了他需要的內容,並在表格上產生了結果,而這正是他所需要的。

一件事情是,對於每條帶有不同“類型”作為字段“標識符”(我稱呼它)的記錄,他都有重復記錄...這是一個示例:

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

...在表格上。 誰能幫我這個忙嗎? 謝謝!

好的,我發現在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]);

我發現了一個示例( 在這里 ),它似乎正是您想要的:
通過訪問將多行中的列值連接到單列中

從上面的鏈接:

問題

最難的部分是為本文提出一個有意義的標題。 這個問題是我在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

這些線路上的某些內容可能適合,但通常不是一個好主意:

   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)

您為什么不嘗試“交叉表查詢”解決方案?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM