Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

"If rst.EOF Then" statement not working.

Posted on 2006-10-23
11
Medium Priority
?
1,993 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 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

927 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