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 Record开发者_C百科set
Set raw = db.OpenRecordset("tblSampleRaw")
If raw.RecordCount > 0 Then
raw.MoveFirst
Do Until raw.EOF
raw.MoveFirst
raw.Delete
Loop
End If
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
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
I'm not a VB programmer, but it looks like 'raw' is going to keep dropping through the loop even after it executes 'MoveFirst'.
How about Dim raw AS DAO.Recordset?
精彩评论