Solved

Run-time error '3167': Record is deleted

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

756 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