no current record after deletion of recordset row?

L

LMC

I've used rs.delete successfully in code before but for some reason in the
code below after the ElseIf criteria is met, the deletion returns a "no
current record" error when it tries to loop to the next row. I tried adding
rs.MoveNext after rs.delete but it returns the same result? I'd appreciate
any suggestions as how to fix it.

Sub flatten_AO5(strTbl As String)
Dim db As Database, strSQL As String, rs As DAO.Recordset
Dim fldDOC As DAO.field, fldRFP As DAO.field
Dim strPrevDOC As String, strPrevRFP As String, strErrField As String
On Error GoTo ErrHandler
'get table content
Set db = CurrentDb
strSQL = "SELECT * FROM " & strTbl & ";"
Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
rs.MoveLast
rs.MoveFirst
'read initial values to be compared
Set fldDOC = rs!DOCid
Set fldRFP = rs!RFPid
strPrevDOC = fldDOC
If IsNull(fldRFP) Then strPrevRFP = "" Else strPrevRFP = fldRFP
'loop through rows to compare field values
rs.MoveNext
Do While Not rs.EOF
'delete rows with invalid criteria
If fldDOC = strPrevDOC Then
If fldRFP = strPrevRFP Then
Debug.Print rs!ID & " = duplicate row deletion"
rs.Delete
ElseIf strPrevRFP = "" Then
rs.MovePrevious
Debug.Print rs!ID & " = previous row deletion"
rs.Delete
End If
Else
Debug.Print rs!ID & " = valid row"
End If
strPrevDOC = fldDOC
strPrevRFP = fldRFP
rs.MoveNext
Loop
Debug.Print "ALL ROWS THAT DID NOT MEET VALID CRITERIA HAVE BEEN DELETED."

ExitHandler:
Set fldDOC = Nothing
Set fldRFP = Nothing
rs.Close
Set rs = Nothing
db.Close
Set db = Nothing
Exit Sub

ErrHandler:
If Err.Number <> 3163 Then
Dim Msg As String
Msg = "Error #" & str(Err.Number) & " was generated by " &
Err.source & Chr(13) & Err.Description
MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
End If
Resume ExitHandler
End Sub

Any replies are appreciated.
LMC
 
L

LMC

I'm not sure why but when I replaced the AND & OR operators in my IF THEN
statement with an embeded IF THEN ElSEIF statement I no longer receive the
"no current record" error message. i.e...

Do While Not rs.EOF
If fldDOC = strPrevDOC Then
If fldRFP = strPrevRFP Then
Debug.Print rs!ID & " = duplicate row deletion"
ElseIf strPrevRFP = "" Then
Debug.Print rs!ID & " = previous row deletion"
rs.MovePrevious
EndIf
rs.Delete
rs.MoveNext
Else
Debug.Print rs!ID & " = valid row"
End If
strPrevDOC = fldDOC
If IsNull(flRFP Then strPrevRFP = " Else strPrevRFP = fldRFP
rs.MoveNext
Loop
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top