Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

To rs.BOF or Not to rs.BOF

Status
Not open for further replies.
Aug 30, 2000
1,177
US
I've seen a lot of sample code both here is the forum and in Microsoft's examples that determine whether or not a recordset is empty by checking if the BOF and EOF flags are both true.

For the most part I see:

Code:
If Not rs.BOF And Not rs.EOF Then
    '<do something here>
End If,

But my contention is that it should be checked this way:

Code:
If Not (rs.BOF And rs.EOF) Then
    '<do something here>
End If

I think the order of operations indicates that the NOT operation will be performed before the AND operation. The resultant AND operation will be:
Code:
    'BOF    'EOF    ANDED    NOT-BOF  NOT-EOF  ANDED-NOTs
      0       0       0         1        1       1
      1       0       0         0        1       0
      0       1       0         1        0       0
      1       1       1         0        0       0
If both the BOF and EOF are true then the recordset is empty. If the BOF Flag is set, then the record pointer is at the begining of the recordset (not the first record). If the EOF is true then the record pointer is at the end of the recordset (not the last record). So the check

Code:
If Not (rs.BOF And rs.EOF) Then
    '<do something here>
End If

ensures operations on non-empty recordsets, but does not position the pointer to any particular record. I agree that the check is mostly conducted directly after the Open method is called but is it also guarenteed that the record pointer is at one end of the other?

Any thoughts?



Mark
 
Because the AND expression is enclosed in parens, it will be evaluated first.

NOT (.EOF AND .BOF) will not work. This will return true in all cases except where both .BOF=True AND .EOF=TRUE.
That means if you at either .EOF or .BOF, but not both, you will still execute the Then portion of the conditional - and you will get a run-time error - either BOF or EOF is true.

EOF BOF EOF AND BOF NOT (EOF AND BOF)
F F F T
F T F T
T F F T
T T T F

I belive the correct boolean logic is

Not (.EOF OR .BOF)

EOF BOF EOF OR BOF NOT (EOF OR BOF)
F F F T
F T T F
T F T F
T T T F

Now you will only read when both EOF and BOF are false.

On the other hand
Not rs.BOF And Not rs.EOF works out as follows:

EOF BOF !EOF !BOF !EOF AND !BOF)
F F T T T
F T T F F
T F F T F
T T F F F

therefore you can see that

Not rs.BOF And Not rs.EOF === Not (rs.BOF OR rs.EOF)
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
You actually want


If Not (rs.BOF And rs.EOF) Then
'<do something here>
End If

Because that does mean some records.

Not rs.BOF And Not rs.EOF === Not (rs.BOF OR rs.EOF)

is easily remembered, and I've been waiting to use this quote for ages.

A string of knots is a knotted string.

Peter Meachem
peter@accuflight.com

 
But to me, this would only indicate that the pointer is currently on a valid record-when both BOF AND EOF are False. And in order to determine, if the recordset is in fact empty, I would have to check to see if the BOF and the EOF flags are both set which only occurs when the recordset does not contain any records.

In the !(BOF or EOF) case, I would only know that the record pointer is on a valid record. Conditions where the recordset contained records, but the marker is on BOF or EOF and could be reset with .MoveFirst would be reported as an empty recordset...




Mark
 
So it really depends on how you're going to use the statement.

If you want to know if its safe to access the RS then use
Not (rs.BOF Or rs.EOF)

If you want to know if the record set is empty, then use
Not (rs.BOF And rs.EOF)

Both are perfectly valid, but mean two different things. Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top