borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2008 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Next Topic (Find record, multiple result) New Topic New Poll Post Reply
AndreaVB Forum : Database : Empty Recordsets (no records) - EOF or both BOF & EOF?
Poster Message
stickleprojects
Level: Moderator


Registered: 09-09-2002
Posts: 954

icon Empty Recordsets (no records) - EOF or both BOF & EOF?

So,
The question is: Which of the following code-snippets is more correct?

if rs.eof then
  msgbox "Recordset is empty"
else
  msgbox "Records found, so carry on " & rs.fields(1).value
end if

or

if rs.eof and rs.bof then
  msgbox "Recordset is empty"
else
  msgbox "Records found, so carry on " & rs.fields(1).value
end if


IMHO.
I'm very much a follower of the open-connection/execute command/store-results/close-connection methods so don't often use the recordset for memory access/storage.

I can't remember the last time i used a bidirectional recordset, so I only ever check for EOF, also, I only tend to need this check immediatly after opening the rs - again i only need to test for EOF.

I have never had a situation where EOF was true but there were records (without first calling movenext/movelast). Can anyone give an example or illustrate when this occurs?


Thoughts?
Kieron

[Edited by stickleprojects on 05-01-2008 at 01:38 AM GMT]

____________________________
Build it better, faster, quicker, easier.. then fix it (non-offical MS mission statement)

05-01-2008 at 01:35 AM
View Profile Send Email to User Show All Posts | Quote Reply
JLRodgers
Level: Moderator

Registered: 04-04-2002
Posts: 1627
icon Re: Empty Recordsets (no records) - EOF or both BOF & EOF?

Can't think of anything offhand as to what would cause it.  But I know that back in 2000-2001 when I was working for a company that there were times (extremely rare, and possibly only on dynamic situations) when a table being opened could cause it to have records, but the EOF or BOF could be true.  For that reason all code had both rs.eof and rs.bof comments.

Some of the data could be retrieved by over 20 people at once, each potentially editing/deleting items -- with the server running a program to populate the tables off and on.  So I suppose that theoretically one user could access a form that opens the table and requests data, and at that exact moment the server truncates the table for the new data, that might cause it (keeping in mind the tables had a few hundred-thousand records).

Basically the test for both was done just to make sure, since there were occasional instances where it'd cause a problem (like a few times a month).


____________________________
Everywhere's Local (classifieds, job postings, & more for everycity in the world - user entered)

06-01-2008 at 05:02 AM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
stickleprojects
Level: Moderator


Registered: 09-09-2002
Posts: 954
icon Re: Empty Recordsets (no records) - EOF or both BOF & EOF?

I'm pretty sure that in the old days, dao had an issue depending on cursor location and type. EOF may have been incorrect until movelast was called?

____________________________
Build it better, faster, quicker, easier.. then fix it (non-offical MS mission statement)

06-01-2008 at 11:47 AM
View Profile Send Email to User Show All Posts | Quote Reply
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 928
icon Re: Empty Recordsets (no records) - EOF or both BOF & EOF?

quote:
stickleprojects wrote:So,
The question is: Which of the following code-snippets is more correct?
if rs.eof then
  msgbox "Recordset is empty"
else
  msgbox "Records found, so carry on " & rs.fields(1).value
end if

or

if rs.eof and rs.bof then
  msgbox "Recordset is empty"
else
  msgbox "Records found, so carry on " & rs.fields(1).value
end if


Of course, if you put it down in this way, the second one.

It's an old question and I've been involved into it many times.
Regardless the fact that every book states to test records' existance by testing both - I've found none even mentioning the single test - we usually assume that we have to know records' existance only when opening a recordset, but it's not so.

If you had to do the above test after a record deletion you could be in EOF condition without being into an empty recordset, so the first test you posted would be wrong. The reason I always suggest the double test is because when I write a checking routine I could call it in many moments, not only after opening a recordset, so the double test is more safe. So I get used to perform the double test anyway, even when I open the recordset in read-only mode.

Only when opening the recordset, if we are in EOF condition right afer opening means the recordset is empty. But if I simply perform a Movelast operation right after opening it, like many people do in order to fast populate ADODCs or other recordset driven controls, I can't be anymore sure that I haven't code that puts me into EOF condition into any recordset event, so at least I should control it. Testing both condition is safer, moreover if I write down the code to perform the existance check now and add the Movelast instruction later, like many do.

As I said, all the books I read teach to do the double test:
    If Not (rs.BOF And rs.EOF) Then
        ' ....there is at least one record
    Else
    ' ElseIf rs.BOF And rs.EOF Then
        ' ....the recordset is empty
    End If

I only found an alternative method, suggested into William R. Vaughn "ADO Examples and Best Practices" performing the double test like following:
    If rs.BOF Or rs.EOF Then
        ' ....there is at least one record
    Else
        ' ....the recordset is empty
    End If
Vaughn says that a positive approach "EOF Or BOF" is less misleading, and maybe he's right, but personally I'm used to use the first method "Not (BOF And EOF)".

I also remark, as I usually do in these debates, that the whole question moves around three keywords to type or not, and I consider this a reason worth to choose without delay the safer, more theorically correct method.
"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding" (Eric S. Raymond - "How To Ask Questions The Smart Way")

Hope it helps.

____________________________
Real Programmer can count up to 1024 on his fingers
07-01-2008 at 10:27 AM
View Profile Send Email to User Show All Posts | Quote Reply
JLRodgers
Level: Moderator

Registered: 04-04-2002
Posts: 1627
icon Re: Empty Recordsets (no records) - EOF or both BOF & EOF?

The dao thing... that could've been why the programs I worked on all had the multiple test.  Some connections were dao, some ado (yet both had the same test).  When I had it they were all converted to ado.



____________________________
Everywhere's Local (classifieds, job postings, & more for everycity in the world - user entered)

12-01-2008 at 11:02 PM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
AndreaVB Forum : Database : Empty Recordsets (no records) - EOF or both BOF & EOF?
Next Topic (Find record, multiple result) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2008 Andrea Tincaniborder