 |
|
 |
JLRodgers Level: Moderator
 Registered: 04-04-2002 Posts: 1627
|
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 |
|
|
yronium Level: Moderator

 Registered: 14-04-2002 Posts: 928
|
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 |
|
|
JLRodgers Level: Moderator
 Registered: 04-04-2002 Posts: 1627
|
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 |
|
|
|
|
 |
 |