从记录集中删除

Deleting from recordset

我有一个子程序,它根据记录集 1 的记录删除记录集 2 中的记录。 该功能有效但速度很慢。 Recordset1 有 300 条记录,Recordset2 有 73000 条记录。

有什么办法可以加快速度吗?

是否可以使用过滤器或重新过滤的记录集?

Public Sub Erase()
    DoCmd.SetWarnings False

    Dim db As DAO.Database
    Dim rs1 As DAO.Recordset
    Dim rs2 As DAO.Recordset
 
    Set db = CurrentDb()
    Set rs1 = db.OpenRecordset("Tbl_1", dbOpenTable)2
    Set rs2 = db.OpenRecordset("KISS_2", dbOpenDynaset)2
    
    If rs1.RecordCount > 0 Then
        rs1.MoveLast
        rs1.MoveFirst
        lngCountRecordsRs1 = rs1.RecordCount
        
        Do Until rs1.EOF
            rs2.MoveFirst
            Do Until rs2.EOF
            If rs1!ID = rs2!ID Then
                    With rs2
                        .Delete
                    End With
            End If
            rs2.MoveNext
            Loop
        rs1.MoveNext
        Loop
    End If
    
    rs2.Close
    rs1.Close
    Set rs2 = Nothing
    Set rs1 = Nothing
    Set db = Nothing

    
Errorhandler:
End Sub

最终我用.FindFirst 解决了这个问题。 感谢您的输入!

          With rs2
                .FindFirst "Id = " & rs1![ID]
                If rs2.NoMatch Then
                Else
                    .Delete
                    Exit Do
                End If
            End With