473,385 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

While loop hitting EOF before last record

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

Expand|Select|Wrap|Line Numbers
  1. Public Sub CheckDupes()
  2.     'Create variables; myNum and numCheck refer to the value in the 'PartNumbers' field
  3.     'cur- first- and lastRec refer to record ID
  4.     Dim RecSet As ADODB.Recordset
  5.     Dim myNum As String
  6.     Dim numCheck As String
  7.     Dim curRec As Long
  8.     Dim firstRec As Long
  9.     Dim lastRec As Long
  10.     Dim rst As Object
  11.     Dim cn As ADODB.Connection
  12.     'Set object variables; rst is only used to find the number of records
  13.     Set RecSet = New ADODB.Recordset
  14.     Set rst = Me.RecordsetClone
  15.     Set cn = New ADODB.Connection
  16.     'Open connection, open RecSet, set lastRec
  17.     cn.Open CurrentProject.Connection
  18.     Call RecSet.Open("tblIandP", cn, adOpenStatic, adLockOptimistic)
  19.     rst.MoveLast
  20.     lastRec = rst.RecordCount - 1
  21.     'Set starting values for first- curRec to avoid skipping loops
  22.     curRec = 3
  23.     firstRec = 1
  24.     'Until last record, get part num and ID
  25.     Do While firstRec < lastRec
  26.         myNum = RecSet.Fields.Item(1).Value
  27.         firstRec = RecSet.Fields.Item(0).Value
  28.         'Move to the last record so the next loop can search backwards
  29.         RecSet.MoveLast
  30.         'Set curRec to avoid skipping inner loop
  31.         curRec = RecSet.Fields.Item(0).Value
  32.         'Until firstRec, get part num and ID
  33.         Do While curRec > firstRec + 1
  34.             numCheck = RecSet.Fields.Item(1).Value
  35.             curRec = RecSet.Fields.Item(0).Value
  36.             'If part nums are same, change Duplicate? value to true
  37.             If numCheck = myNum Then
  38.                 Call RecSet.Update("Duplicate?", True)
  39.             End If
  40.             'Move up the list
  41.             RecSet.MovePrevious
  42.         Loop
  43.         'Move to the next record to be checked
  44.         RecSet.MoveNext
  45.     Loop
  46. End Sub
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!
Jun 26 '15 #1

✓ answered by jforbes

Hey RadioWriter,

Off the top of my head, you may want to .MoveLast before rst.RecordCount as I don't think RecordCount is 100% reliable until the recordset is navigated to the end of the Recordset.

Also, how do you know RecSet.Fields.Item(0).Value will be sequential when you are iterating through the records. If your recordset isn't ordered on this column I'm pretty sure it will break.

So I need to ask, is this Code something you inherited or something you are writing? Because if you are writing it, I think there is a less error prone way to go about this.

4 3471
jforbes
1,107 Expert 1GB
Hey RadioWriter,

Off the top of my head, you may want to .MoveLast before rst.RecordCount as I don't think RecordCount is 100% reliable until the recordset is navigated to the end of the Recordset.

Also, how do you know RecSet.Fields.Item(0).Value will be sequential when you are iterating through the records. If your recordset isn't ordered on this column I'm pretty sure it will break.

So I need to ask, is this Code something you inherited or something you are writing? Because if you are writing it, I think there is a less error prone way to go about this.
Jun 26 '15 #2
Edit- Yes, this worked. Very sorry.
Thank you for the help!
Jun 26 '15 #3
jforbes
1,107 Expert 1GB
Glad you got it going!

You may want to take the Question Mark (?) out of your Fieldnames as it a reserved character in SQL. It may cause you trouble down the road.

Also, another approach you may want to consider for this is to run a single update query against the Records to calculate and update your Duplicates Flag. It might be less complicated and probably quicker as you are executing one statement against the database instead of multiple updates.

I mocked this up, it's not the best approach as it's using a Dlookup, but even using a DLookup I have a feeling it will run faster than the looping approach.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command6_Click()
  2.     Dim sSQL As String
  3.     sSQL = "UPDATE DuplicatesTest SET "
  4.     sSQL = sSQL & "  Duplicate = "
  5.     sSQL = sSQL & "    (DCount(""ID"",""DuplicatesTest"",""[ID]>"" & [ID] & "" AND [PartNumber]='"" & [PartNumber] & ""'"")>0)"
  6.     CurrentDb.Execute sSQL
  7. End Sub
Jun 26 '15 #4
Well, definitely faster. (Looping clocks in around 1.5 minutes to go down the list and then back up, but the above method takes only 10 seconds.)

Unfortunately, the return is necessary to flag all of the initial entries being checked against. Another field contains prices which need to be compared to find the current/correct one. Once they've been properly updated, the excess entries will be deleted and another method will be used to change the current price/add new entries such that no more duplicates can be created.

Again, thank you SO much for your help. It is very much appreciated.
Jun 26 '15 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: vasilijepetkovic | last post by:
Hello All, I have a problem with the program that should generate x number of txt files (x is the number of records in the file datafile.txt). Once I execute the program (see below) only one...
23
by: ern | last post by:
I have a program that runs scripts. If the user types "script myScript.dat" the program will grab commands from the text file, verify correctness, and begin executing the script UNTIL... I need...
4
by: Rico | last post by:
Hi All, Just wondering, in vb code, how to if the last record on a cascading form is the current record? Thanks!
2
by: Kenneth | last post by:
Ok, so logically this code seems to make sense but for some reason it reads the last record twice before hitting eof. Is ifstream.eof() implementation dependent? void LoadFile(ifstream &File,...
2
by: Bhujanga | last post by:
I want to scroll through records on an open form and take certain actions based on various criteria. So I want to have a loop such as this: Do While Not Last Record <----- ? ....actions.... ...
1
clintw
by: clintw | last post by:
Hi, I need someone to please clarify the meaning of the following ASP lines. Having trouble following ASP's structure and not finding relevant explanations on web. 1. While ((Repeat1__numRows...
4
anfetienne
by: anfetienne | last post by:
hi im back again.......i have a code to create strings and save it within a text file to pass variables to flash. im using the string format below. ...
1
LeighW
by: LeighW | last post by:
Hello, I used Allen Browne's method of assigning default values from the last record which is especially helpful when adding a new record to filtered records. Using his method I had to create a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.