Solved

Run-time error '3167': Record is deleted

Posted on 2009-07-13
5
1,006 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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…

762 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

21 Experts available now in Live!

Get 1:1 Help Now