Kopieren Sie Anhänge von Datensätzen in einem Recordset in Datensätze in einem anderen mit VBA in MS Access

Kopieren Sie Anhänge von Datensätzen in einem Recordset in Datensätze in einem anderen mit VBA in MS Access

Ich habe es fast zum Laufen gebracht. Ich glaube, ich brauche wirklich nur noch einen zweiten Blick darauf. Ich glaube, mein Problem ist nur ein einfaches Problem mit der Programmierstruktur. Entweder zu viele Schleifen oder das Öffnen/Schließen von Recordsets in der falschen Reihenfolge.

Ich versuche, alle in Datensätzen eines DAO-Recordsets enthaltenen Anhangsdateien in entsprechende Datensätze eines anderen DAO-Recordsets zu kopieren. Beide Recordsets beziehen Daten aus derselben Tabelle. Das erste Recordset (rstOld) enthält Datensätze mit einem Datumswert aus dem letzten Jahr und diese Datensätze könnten eine beliebige Anzahl von Anhängen enthalten. Das zweite Recordset (rstNew) enthält Datensätze mit einem Datumswert aus diesem Jahr und diese Datensätze enthalten keine Anhänge.

Um dies zu erreichen, starte ich eine Schleife durch jeden Datensatz in rstNew. Für jeden Datensatz in rstNew sammle ich den Wert des Felds Name und starte dann eine zweite Schleife. Die zweite Schleife findet einen Datensatz in rstOld mit einem passenden Feld Name. Von dort aus muss ich nur alle Anhänge aus dem Datensatz in rstOld in den Datensatz in rstNew kopieren.

Das Merkwürdige ist, dass es beim ersten Datensatz in rstNew, für den es eine Übereinstimmung findet, korrekt funktioniert. Danach funktioniert es für alle nachfolgenden Datensätze nicht mehr.

Hier ist mein bisheriger Code:

    Dim db As Database
    Dim strOldSQL As String
    Dim rstOld As DAO.Recordset2
    Dim strNewSQL As String
    Dim rstNew As DAO.Recordset2
    Dim rstOldAttachments As DAO.Recordset2
    Dim rstNewAttachments As DAO.Recordset2
    Dim strCurrentSiteName As String
    Dim strOldSiteName As String
Set db = CurrentDb()

    'First let's open a recordset that contains all of the records from this year.
    strNewSQL = "SELECT tblAuditForms.SiteName, tblAuditForms.Attachments, tblAuditForms.AuditYear FROM tblAuditForms WHERE AuditYear = #" & Format(cboMyDate, "mm/dd/yyyy") & "# ORDER By tblAuditForms.SiteName;"
    Set rstNew = db.OpenRecordset(strNewSQL)
    rstNew.MoveFirst
    rstNew.Edit
    
    Do While Not rstNew.EOF 'Now we need to loop through these records.
    
        strCurrentSiteName = rstNew.Fields("SiteName").Value 'Get the name of the site for the current record that we're on. We'll use this to compare with the sites in the previous audit.
                    
        'Now let's open a recordset that contains all records from the previous audit.
        strOldSQL = "SELECT tblAuditForms.SiteName, tblAuditForms.Attachments, Year([AuditYear]) FROM tblAuditForms WHERE Year([AuditYear]) = " & Me.cboPreviousDate & " ORDER BY tblAuditForms.SiteName;"
        Set rstOld = db.OpenRecordset(strOldSQL)
        rstOld.MoveFirst
        
        Do While Not rstOld.EOF 'Loop through each of the records from the previous audit until we find a record that matches the current site name.
        
            strOldSiteName = rstOld.Fields("SiteName").Value
        
            If strCurrentSiteName = strOldSiteName Then 'If this is true, then we've found a record from the previous audit that matches the one from our current audit.
                'Now it's just a matter of copying the attachments from the old record into the new one.  Working with attachments is annoying though.
                
                'This next block should loop through the attachments (if any) in the old record and copy them into the new record.
                Set rstOldAttachments = rstOld.Fields("Attachments").Value
                rstOldAttachments.MoveFirst
                
                Set rstNewAttachments = rstNew.Fields("Attachments").Value

                Do While Not rstOldAttachments.EOF
                    
                    rstNewAttachments.AddNew
                    rstNewAttachments.Fields("FileData").Value = rstOldAttachments.Fields("FileData").Value
                    rstNewAttachments.Fields("FileName").Value = rstOldAttachments.Fields("FileName").Value
                    rstNewAttachments.Fields("FileType").Value = rstOldAttachments.Fields("FileType").Value
                    rstNewAttachments.Update
                
                    rstOldAttachments.MoveNext
                Loop
                
                'Now that we've found the site from the previous audit and copied its attachments into the new record we can close the old recordset and move onto the next site in the current audit.
                rstOldAttachments.Close
                rstNewAttachments.Close
                Exit Do
            
            End If
                        
            rstOld.MoveNext
        Loop
         
        rstOld.Close
        rstNew.Update
        rstNew.MoveNext
    Loop
        
    'If we've gotten this far then we've looped through all of the new records that we just created from the weekly staffing workbook.
    rstNew.Close
    
    

Wie ich bereits sagte, funktioniert dieser Code in der ersten Schleife durch den rstNew-Recordset, aber nicht in den nachfolgenden Schleifen. Breche ich zu früh aus einer Schleife aus? Oder schließe ich einen Recordset zu früh?

Antwort1

Ich habe es herausgefunden! Ich wusste, dass ich nah dran war. Ich habe gelernt, dass die Recordset-Editmode-Eigenschaft auf 0 zurückgesetzt wird, sobald Sie die Anweisung „recordset.update“ (oder in meinem Fall „rstNew.update“) ausgeführt haben. Das würde erklären, warum die Anhänge in der ersten Schleife erfolgreich kopiert wurden, in den folgenden Schleifen jedoch ein Fehler auftrat. Ich musste also nur die Anweisung „rstNew.Edit“ direkt über die Zeile „Set rstNewAttachments = rstNew.Fields(„Attachments“).Value“ verschieben.

So sieht der neue Code aus:

    Dim db As Database
    Dim strOldSQL As String
    Dim rstOld As DAO.Recordset2
    Dim strNewSQL As String
    Dim rstNew As DAO.Recordset2
    Dim rstOldAttachments As DAO.Recordset2
    Dim rstNewAttachments As DAO.Recordset2
    Dim strCurrentSiteName As String
    Dim strOldSiteName As String
Set db = CurrentDb()

    'First let's open a recordset that contains all of the records from this year.
    strNewSQL = "SELECT tblAuditForms.SiteName, tblAuditForms.Attachments, tblAuditForms.AuditYear FROM tblAuditForms WHERE AuditYear = #" & Format(cboMyDate, "mm/dd/yyyy") & "# ORDER By tblAuditForms.SiteName;"
    Set rstNew = db.OpenRecordset(strNewSQL)
    rstNew.MoveFirst
        
    Do While Not rstNew.EOF 'Now we need to loop through these records.
    
        strCurrentSiteName = rstNew.Fields("SiteName").Value 'Get the name of the site for the current record that we're on. We'll use this to compare with the sites in the previous audit.
                    
        'Now let's open a recordset that contains all records from the previous audit.
        strOldSQL = "SELECT tblAuditForms.SiteName, tblAuditForms.Attachments, Year([AuditYear]) FROM tblAuditForms WHERE Year([AuditYear]) = " & Me.cboPreviousDate & " ORDER BY tblAuditForms.SiteName;"
        Set rstOld = db.OpenRecordset(strOldSQL)
        rstOld.MoveFirst
        
        Do While Not rstOld.EOF 'Loop through each of the records from the previous audit until we find a record that matches the current site name.
        
            strOldSiteName = rstOld.Fields("SiteName").Value
        
            If strCurrentSiteName = strOldSiteName Then 'If this is true, then we've found a record from the previous audit that matches the one from our current audit.
                'Now it's just a matter of copying the attachments from the old record into the new one.  Working with attachments is annoying though.
                
                'This next block should loop through the attachments (if any) in the old record and copy them into the new record.
                Set rstOldAttachments = rstOld.Fields("Attachments").Value
                rstOldAttachments.MoveFirst
                
                rstNew.Edit
                Set rstNewAttachments = rstNew.Fields("Attachments").Value

                Do While Not rstOldAttachments.EOF
                    
                    rstNewAttachments.AddNew
                    rstNewAttachments.Fields("FileData").Value = rstOldAttachments.Fields("FileData").Value
                    rstNewAttachments.Fields("FileName").Value = rstOldAttachments.Fields("FileName").Value
                    rstNewAttachments.Fields("FileType").Value = rstOldAttachments.Fields("FileType").Value
                    rstNewAttachments.Update
                
                    rstOldAttachments.MoveNext
                Loop
                
                'Now that we've found the site from the previous audit and copied its attachments into the new record we can close the old recordset and move onto the next site in the current audit.
                rstOldAttachments.Close
                rstNewAttachments.Close
                Exit Do
            
            End If
                        
            rstOld.MoveNext
        Loop
         
        rstOld.Close
        rstNew.Update
        rstNew.MoveNext
    Loop
        
    'If we've gotten this far then we've looped through all of the new records that we just created from the weekly staffing workbook.
    rstNew.Close

verwandte Informationen