Search code examples
ms-accessvbaloopsrecordset

Deleting rows from a table using a loop


Why does my Do Until loop try to run raw.Delete even though raw.EOF is true? If I have an empty table, this crashes. Why?

Dim raw As Recordset
Set raw = db.OpenRecordset("tblSampleRaw")

If raw.RecordCount > 0 Then
    raw.MoveFirst

    Do Until raw.EOF
        raw.MoveFirst
        raw.Delete
    Loop
End If

Solution

  • I'm not sure, or a VBA expert, but how come you are constantly doing a MoveFirst? Your never moving forward in the recordset. Try

    Do Until raw.EOF
            raw.Delete
            raw.MoveNext
     Loop