Solved

"If rst.EOF Then" statement not working.

Posted on 2006-10-23
11
1,940 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 143

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 143

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 143

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 143

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
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…

735 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