mobuns
Goto Top

Access Datensätze zusammenfassen

Hallo zusammen,

ich habe ein Problemchen, ich habe eine Tabelle mit Datensätzen.

Einige Datensätze haben in einem Feld den gleichen Wert stehen.

Jetzt sollen alle Datensätze die einen gleichen Wert haben zu einem zusammengefügt werden.(Ein bestimmtes Feld aus dem Datensatz)

Content-Key: 221074

Url: https://administrator.de/contentid/221074

Printed on: April 20, 2024 at 01:04 o'clock

Member: colinardo
colinardo Nov 04, 2013 at 09:32:25 (UTC)
Goto Top
Hallo JokerM,
da wäre der Duplicate Query Wizard von Access das Mittel der Wahl:http://www.howtogeek.com/howto/microsoft-office/create-a-query-in-micro ...

Grüße Uwe
Member: SlainteMhath
SlainteMhath Nov 04, 2013 at 09:35:21 (UTC)
Goto Top
Moin,

also genau versteh ich dein Anliegen nicht, aber evtl. kannst du das nochmal anhand eines Beispiels erklären?

meinst Du evtl. sowas:
INSERT INTO neue_tabelle 
SELECT * FROM alte_tabelle GROUP BY feldname

lg,
Slainte
Member: mobuns
mobuns Nov 04, 2013 updated at 14:01:45 (UTC)
Goto Top
Ok dann versuche ich das nochmal an einem Beispiel zu erläutern.

es gibt eine Tabelle, in dieser Tabelle gibt es 2 Felder Zahl und Text.

In dem Feld Zahl kann die selbe Zahl öfter vorkommen.

Jetzt sollen alle Textfelder von Datensätzen die den gleichen Wert im Feld Zahl haben zu einem zusammengefügt werden.

Also

Zahl Text Text1 Text2

ich hoffe das hilft weiter

Gruß
Member: colinardo
colinardo Nov 04, 2013 at 17:56:57 (UTC)
Goto Top
Probiers mal hiermit:
(in Zeile 2 den Spaltennahmen der Zahl-Spalte, in Zeile 3 den für die Textspalte, und in Zeile 4 den Namen der Tabelle angeben)
Sub mergeDuplicates()
    Const COLZAHL = "intZahl"  
    Const COLTEXT = "strText"  
    Const TBLTABELLE = "Testtabelle"  
    Dim rs As Recordset, rs2 As Recordset
    dupeSQL = "SELECT DISTINCT " & COLZAHL & " from " & TBLTABELLE  
    Set rs = CurrentDb.OpenRecordset(dupeSQL)
    
    While Not rs.EOF
        concatText = ConcatRelated(COLTEXT, TBLTABELLE, COLZAHL & " = " & rs(0).Value)  
        Set rs2 = CurrentDb.OpenRecordset("Select " & COLZAHL & "," & COLTEXT & " FROM " & TBLTABELLE & " WHERE " & COLZAHL & " = " & rs(0).Value)  
        rs2.MoveFirst
        rs2.Edit
        rs2(COLTEXT).Value = concatText
        rs2.Update
        rs2.MoveNext
        While Not rs2.EOF
            rs2.Delete
            rs2.MoveNext
        Wend
        rs2.Close
        rs.MoveNext
    Wend
    rs.Close
End Sub

' Function from http://allenbrowne.com/func-concat.html  
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

Grüße Uwe