Solved

"If rst.EOF Then" statement not working.

Posted on 2006-10-23
11
1,929 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]
ID: 17787047
change the line to this:
If rst.EOF and rst.BOF Then
0
 
LVL 8

Author Comment

by:infolurk
ID: 17787083
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]
ID: 17787103
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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 8

Author Comment

by:infolurk
ID: 17787132
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
ID: 17787149
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
 
LVL 2

Expert Comment

by:AngelinoM
ID: 17787320
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]
ID: 17787518
just to note that angelinoM's suggestion will, unless the recordset is empty, produce infinite looping!
0
 
LVL 20

Expert Comment

by:clarkscott
ID: 17787589
not 'If rst.EOF and rst.BOF  instead 'If rst.EOF OR rst.BOF


Scott C.

0
 
LVL 34

Expert Comment

by:jefftwilley
ID: 17788721
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
ID: 17791663
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
ID: 17800751
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

773 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