Solved

"If rst.EOF Then" statement not working.

Posted on 2006-10-23
11
1,924 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
 
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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

920 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

17 Experts available now in Live!

Get 1:1 Help Now