I'm working on building a database in Access 2013, and I'm coming across an error in a subroutine which checks to see if there are duplicate entries in the field PartNumbers (and marks a True/False field as true if an entry is a duplicate).
I'm getting Run-Time Error '3201': "Either BOF or EOF is True, or the current record has been deleted. Requested operation requires a current record." This error occurs on line 25 after 2 loops (curRec = 4, firstRec = 2 - firstRec is about to be updated). The code works fine for the section it runs (records 2 and 3 are duplicates), but there are over 3.5k records. I'm not entirely sure why it's hitting EOF with the precautions I've taken to avoid skipping the inner loop. I've also tried replacing the ADODB Recordset with a regular one, and get the same results.
Thank you in advance for your help!
Code:
Public Sub CheckDupes()
'Create variables; myNum and numCheck refer to the value in the 'PartNumbers' field
'cur- first- and lastRec refer to record ID
Dim RecSet As ADODB.Recordset
Dim myNum As String
Dim numCheck As String
Dim curRec As Long
Dim firstRec As Long
Dim lastRec As Long
Dim rst As Object
Dim cn As ADODB.Connection
'Set object variables; rst is only used to find the number of records
Set RecSet = New ADODB.Recordset
Set rst = Me.RecordsetClone
Set cn = New ADODB.Connection
'Open connection, open RecSet, set lastRec
cn.Open CurrentProject.Connection
Call RecSet.Open("tblIandP", cn, adOpenStatic, adLockOptimistic)
rst.MoveLast
lastRec = rst.RecordCount - 1
'Set starting values for first- curRec to avoid skipping loops
curRec = 3
firstRec = 1
'Until last record, get part num and ID
Do While firstRec < lastRec
myNum = RecSet.Fields.Item(1).Value
firstRec = RecSet.Fields.Item(0).Value
'Move to the last record so the next loop can search backwards
RecSet.MoveLast
'Set curRec to avoid skipping inner loop
curRec = RecSet.Fields.Item(0).Value
'Until firstRec, get part num and ID
Do While curRec > firstRec + 1
numCheck = RecSet.Fields.Item(1).Value
curRec = RecSet.Fields.Item(0).Value
'If part nums are same, change Duplicate? value to true
If numCheck = myNum Then
Call RecSet.Update("Duplicate?", True)
End If
'Move up the list
RecSet.MovePrevious
Loop
'Move to the next record to be checked
RecSet.MoveNext
Loop
End Sub
Thank you in advance for your help!
Comment