Solved

Run-time error '3167': Record is deleted

Posted on 2009-07-13
5
1,014 Views
Last Modified: 2013-12-20
I am trying to execute a loop that deletes records based on certain criteria. My intent is to go to the end of the recordset, store a concatenation of 2 field values in a variable, move to the previous record and store the same two field values in another variable and compare the two. If they are the same, then delete the current record. Then repeat the process until you get to the beginning of the recordset. But I get an error "Run-time error '3167': Record is deleted". I assume this is because it is trying to store the field values of a deleted record. How do I wok around this?
Set qd = db.CreateQueryDef("ReviewStats", sql)

Set td = db.CreateTableDef("TempReviewStats")

td.Fields.Append td.CreateField("Role", dbText, 10)

td.Fields.Append td.CreateField("Protocol", dbText, 10)

td.Fields.Append td.CreateField("Reviewer", dbText, 50)

td.Fields.Append td.CreateField("RecDat", dbDate, 20)

td.Fields.Append td.CreateField("ComplDat", dbDate, 20)
 

db.TableDefs.Append td
 

db.Execute ("INSERT INTO TempReviewStats SELECT * FROM ReviewStats;")
 

Set rs = td.OpenRecordset
 

rs.MoveLast
 

Do Until rs.BOF

    dtTest1 = Trim(rs!Reviewer) & Trim(rs!RecDat)

    rs.MovePrevious

    dtTest2 = Trim(rs!Reviewer) & Trim(rs!RecDat)

    If dtTest1 = dtTest2 Then

        rs.Delete

    End If

Loop

Open in new window

0
Comment
Question by:NashVegas
  • 3
  • 2
5 Comments
 
LVL 16

Accepted Solution

by:
Chuck Wood earned 500 total points
ID: 24843069
You need to move previous again to skip over the deleted record.
Set qd = db.CreateQueryDef("ReviewStats", sql)

Set td = db.CreateTableDef("TempReviewStats")

td.Fields.Append td.CreateField("Role", dbText, 10)

td.Fields.Append td.CreateField("Protocol", dbText, 10)

td.Fields.Append td.CreateField("Reviewer", dbText, 50)

td.Fields.Append td.CreateField("RecDat", dbDate, 20)

td.Fields.Append td.CreateField("ComplDat", dbDate, 20)

 

db.TableDefs.Append td

 

db.Execute ("INSERT INTO TempReviewStats SELECT * FROM ReviewStats;")

 

Set rs = td.OpenRecordset

 

rs.MoveLast

 

Do Until rs.BOF

    dtTest1 = Trim(rs!Reviewer) & Trim(rs!RecDat)

    rs.MovePrevious

    dtTest2 = Trim(rs!Reviewer) & Trim(rs!RecDat)

    If dtTest1 = dtTest2 Then

        rs.Delete

        rs.MovePrevious

    End If

Loop

Open in new window

0
 

Author Comment

by:NashVegas
ID: 24843208
I thought as much and tried that previously. However, I then get this message:
Run-time error '3021': No current record
...and the line "dtTest2 = Trim(rs!Reviewer) & Trim(rs!RecDat)" is highlighted. Ah, but I just realized why this is happening. It is because dtTest1 is already the first record and then it moves to previous and is out of range. So I need to modify my loop to prevent this. Is this the best way?:
Do Until rs.BOF
    dtTest1 = Trim(rs!Reviewer) & Trim(rs!RecDat)
    rs.MovePrevious
    If Not rs.BOF Then
        dtTest2 = Trim(rs!Reviewer) & Trim(rs!RecDat)
        If dtTest1 = dtTest2 Then
            rs.Delete
            rs.MovePrevious
        End If
    End If
Loop
0
 
LVL 16

Expert Comment

by:Chuck Wood
ID: 24843290
Either that or put the BOF test before the MovePrevious
-chuck wood
Do Until rs.BOF

    dtTest1 = Trim(rs!Reviewer) & Trim(rs!RecDat)

    rs.MovePrevious

    dtTest2 = Trim(rs!Reviewer) & Trim(rs!RecDat)

    If dtTest1 = dtTest2 Then

        rs.Delete

        If Not rs.BOF Then rs.MovePrevious

    End If

Loop

Open in new window

0
 

Author Comment

by:NashVegas
ID: 24843394
Actually, that does not work because that is doing the same thing the "Do Unitl rs.BOF" statement is doing. When it is at the first record, the "If Not rs.BOF" validation still passes. It then re-enters the loop, concatenates the fields and then tries to move to the previous record which is out of range. So I have to test right after the first "rs.MovePrevious" statement within the loop.
0
 
LVL 16

Expert Comment

by:Chuck Wood
ID: 24843455
Yes, that makes sense. Goo dluck on your project.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now