1

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

4 Answers 4

3

You are doing a Row-By-Agonizing-Row or RBAR (reebar) operation. The larger the table, the more time this is going to take.

Most databases can work more efficiently than RBARs. I suggest you think in terms of SETS rather than rows.

I think you should replace that entire block of code with this:

DoCmd.RunSQL "DELETE * FROM tblSampleRaw"

for purposes of the answer

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

If raw.RecordCount > 0 Then
    raw.MoveFirst

    WHILE NOT raw.EOF or raw.BOF
        raw.MoveFirst
        raw.Delete
    Loop
End If
Sign up to request clarification or add additional context in comments.

4 Comments

You are probably right. But a TRUNCATE would even be quicker. I'm just curious why this loop doesn't work though.
If you're using Jet/ACE, it processes "DELETE * FROM table" as TRUNCATE.
I think that inside the loop .MoveFirst should be .MoveNext and should be after the Delete.
@David W. Fenton: What is the * in "DELETE * FROM Table" supposed to do. Delete all columns as opposed to... what?
2

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

7 Comments

Move to first record, delete it, then what is first? The next record I would hope
Well if that was true you would never need to do a MoveFirst then would you? You do it initially outside your loop so you should always be on the first record.
Also I don't think you hit EOF until you move passed the last record.
you just took the words out of my mouth! +1
The deleted record is not removed from the recordset unless you requery it. You can see this in a continuous or datasheet form in Access if you elsewhere delete a record displaying in the form the record selector and all the controls for that record will remain in the form, but with #DELETED in all the controls.
|
0

I'm not a VB programmer, but it looks like 'raw' is going to keep dropping through the loop even after it executes 'MoveFirst'.

Comments

0

How about Dim raw AS DAO.Recordset?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.