2009-09-29 72 views
2

好吧,一個在工作的人有一個小的訪問數據庫,他用它來跟蹤事情。他有這種形式,他使用已經查詢他需要什麼,並在表單上產生結果,這是他所需要的。MS Access 2003 - 在表單上連接相同ID的字段類型

一件事是,他已經重複所有以不同的「類型」作爲場「indentifier」(我所說的話)出現記錄......這裏有一個例子:

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 

...在窗體上。任何人都可以幫助我嗎?謝謝!

+0

在你的例子中'B'和'C'都有'ID 1'。那是對的嗎? – 2009-09-29 11:31:01

+0

哎呀,不應該是每個人都有不同的ID號碼...對不起! – Justin 2009-09-30 02:26:12

回答

2

我發現了一個例子(here),這似乎是你正在尋找什麼爲:
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 
+0

謝謝....這正是我所期待的 – Justin 2009-09-30 02:27:05

+0

感謝鏈接 – 2009-09-30 05:10:31

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)

+0

是的,我明白了,但是對於這個人需要它的事實上是有道理的,因爲他的需求並不是很好,並且對於該事件代碼的每個Code#和Descrip,他需要查看可能適用的每個「實例類型」 ....基本上就是這樣。 我知道串聯就像關係式回溯,但是...... ?? – Justin 2009-09-30 02:29:50

+0

感謝您一如既往的幫助! – Justin 2009-09-30 02:30:24

+0

我想你不能總是贏得:)。不用客氣。 – Fionnuala 2009-09-30 09:30:16

3

OK,我發現在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]); 
+0

甜美的例子。非常感謝! – Justin 2009-09-30 02:31:15

0

+0

交叉表爲每個值創建一個列,而不是單個字段中的分隔值列表。它也會產生可變數量的列(除非你巧妙地設計你的SQL來使用查找表作爲列標題源)。 – 2009-09-30 02:54:16

+0

我同意你的限制。這是一個'可能'的解決方案,取決於原來的情況。 – 2009-09-30 05:09:11