Solved

"If rst.EOF Then" statement not working.

Posted on 2006-10-23
11
1,919 Views
Last Modified: 2012-06-27
I have a problem that has cropped up recently. It seems to be a bug, but someone may have an idea. This code used to work.

'Declarations and recordset population

start:

If rst.EOF Then
    rst.Close
    Set rst = Nothing
    Set myDB = Nothing
    Exit Sub
End If
rst.MoveNext                                 'go to next record in recordset,
strDrillhole2 = rst!HOLE_NUMBER              'set strDrillhole2 to HOLE_NUMBER
sngVoid = rst!ASSAY_FROM                     'and sngVoid to ASSAY_FROM
rst.MovePrevious

'other data processing with goto start calls at various intervals

Basically when the code is getting to the end of the recordset, it is not going into the 'If rst.EOF Then' call, hitting the 'rst.MoveNext' call and coming up with a record not found error at 'strDrillhole2 = rst!HOLE_NUMBER'.
rst.EOF is being set to true, but the If statement is not being triggered. This has got me stuffed.

Cheers
Steve
0
Comment
Question by:infolurk
11 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
change the line to this:
If rst.EOF and rst.BOF Then
0
 
LVL 8

Author Comment

by:infolurk
Comment Utility
The check is done every time the code returns to start, which is mulitple times each time the code is run.
rst.EOF is being set to true, when the error is occuring I can check what all the variables are set to. For whatever reason, even thought rst.EOF is true the 'If rst.EOF Then' statement is not being entered. The Exit Sub statement is not being reached and an error is occuring when the recordset is trying to assign strDrillhole2 = rst!HOLE_NUMBER after the end of file has been reached.
I did the right thing and tried 'If rst.EOF and rst.BOF Then', just in case it worked. rst.EOF is being set to true and of course rst.BOF is false, as the recordset is populated and I am checking for the end of the recordset. Still, I suppose it worked just as well as my current code. It ignored my if statement perfectly.

Cheers
Steve
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
I see a potential issue:

start:

If rst.EOF Then
    rst.Close
    Set rst = Nothing
    Set myDB = Nothing
    Exit Sub
End If

rst.MoveNext                                 'go to next record in recordset,  <<< this one is "wrong" here
strDrillhole2 = rst!HOLE_NUMBER              'set strDrillhole2 to HOLE_NUMBER
sngVoid = rst!ASSAY_FROM                     'and sngVoid to ASSAY_FROM
rst.MovePrevious                                <<< you go "next" above, and here a MovePrevious ???


Can you explain what you are really trying to do?


usually, to read the whole recordset and process each record:

while not rst.eof
  strDrillhole2 = rst!HOLE_NUMBER              'set strDrillhole2 to HOLE_NUMBER
   sngVoid = rst!ASSAY_FROM                     'and sngVoid to ASSAY_FROM

  'work with the variables/values, process...

  'go to next record in recordset
  rst.MoveNext                                
wend

rst.Close
Set rst = Nothing
Set myDB = Nothing


0
 
LVL 8

Author Comment

by:infolurk
Comment Utility
The code processes a fairly large recordset and depending on a number of user assigned variables grabs records and writes out calculated values.
If uses goto's, which I hate and has some fairly dodgy stuff going on. I am tidying it up at the moment.
For instance, the only reason it has been "working" up till now is due to this little gem of a line; On Error GoTo finish
Finish, of course, is where the recordsets and database declarations are closed and set to nothing then the sub is ended.
The origional code snippet, before I started working on it was;
If rst.EOF = True Then
       GoTo finish
    End If
    rst.MoveNext
    strDrillhole2 = rst!HOLE_NUMBER
    sngVoid = rst!ASSAY_FROM
    rst.MovePrevious

This if statement is never entered either, the function ends because it generates an error at 'strDrillhole2 = rst!HOLE_NUMBER' and the 'On Error GoTo finish' does the job most of the time if there are no problems with the data.
Fair enough if I have to completely rewrite this, but I dont see why the if statement isnt being entered when the rst.EOF is evaluating as true. I will have to write a number of do while loops to redo this without the if statement.

Cheers
Steve
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
to explain the problem:

rs.eof = false  -> means you still are at a record. it might be the last one in the recordset, but eof will return false
rs.movenext   -> will try to move to the next record. it you where at the last record, eof changes to true and you don't have a current record

as commonly used, eof means you are AFTER the last data/record, not at the last record itself.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 2

Expert Comment

by:AngelinoM
Comment Utility
Try:

While not rst.EOF
   rst.MoveNext                                 'go to next record in recordset,
   strDrillhole2 = rst!HOLE_NUMBER              'set strDrillhole2 to HOLE_NUMBER
   sngVoid = rst!ASSAY_FROM                     'and sngVoid to ASSAY_FROM
   rst.MovePrevious
wend
rst.Close
Set rst = Nothing
Set myDB = Nothing
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
just to note that angelinoM's suggestion will, unless the recordset is empty, produce infinite looping!
0
 
LVL 20

Expert Comment

by:clarkscott
Comment Utility
not 'If rst.EOF and rst.BOF  instead 'If rst.EOF OR rst.BOF


Scott C.

0
 
LVL 34

Expert Comment

by:jefftwilley
Comment Utility
Shouldn't it read differently? When do you ever move to the first record?

If rst.EOF Then
    rst.Close
    Set rst = Nothing
    Set myDB = Nothing
    Exit Sub
End If
rst.MoveFIRST                                'go to next record in recordset,
strDrillhole2 = rst!HOLE_NUMBER              'set strDrillhole2 to HOLE_NUMBER
sngVoid = rst!ASSAY_FROM                     'and sngVoid to ASSAY_FROM
rst.MovePrevious

then down here
rst.movenext   <---Error Trap here to move to "finish"
0
 
LVL 15

Expert Comment

by:MNelson831
Comment Utility
I skimmed most of the answers, so this may be posted and I missed it.  If so, just ignore me:

I prefer this route:

If rst.Recordcount = 0 Then
    rst.Close
    Set rst = Nothing
    Set myDB = Nothing
    Exit Sub
End If
rst.MoveFirst
Do until rs.eof
     strDrillhole2 = rst!HOLE_NUMBER              'set strDrillhole2 to HOLE_NUMBER
     sngVoid = rst!ASSAY_FROM                     'and sngVoid to ASSAY_FROM
     rst.MoveNext
Loop



Unless you are deliberately moving through them backwards then just reverse the directions:

If rst.Recordcount = 0 Then
    rst.Close
    Set rst = Nothing
    Set myDB = Nothing
    Exit Sub
End If
rst.MoveLast
Do until rs.bof
     strDrillhole2 = rst!HOLE_NUMBER              'set strDrillhole2 to HOLE_NUMBER
     sngVoid = rst!ASSAY_FROM                     'and sngVoid to ASSAY_FROM
     rst.MovePrevious
Loop
0
 
LVL 8

Author Comment

by:infolurk
Comment Utility
The proverbial light went off in my head when I read angels explanation

rs.eof = false  -> means you still are at a record. it might be the last one in the recordset, but eof will return false
rs.movenext   -> will try to move to the next record. it you where at the last record, eof changes to true and you don't have a current record

When the error was happening the debugger was going to the movenext line. I was looking at the rst.EOF and seeing it set to true, thinking that the if statement was being ignored. Of course, the rst.EOF was only being set to true AFTER the code went past the if statement, at the movenext line. I wasnt thinking logically. Thanks to everyone who answered. To answer a few of your replies, the Start: is an insertion point called from 5 different 'if then goto' statements further down in the procedure. I will probably end up removing all the gotos and wrapping the code up in a few do while loops.

Cheers
Steve
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now