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
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