Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Run-time error '3167': Record is deleted

Posted on 2009-07-13
5
1,026 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: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

856 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