Reading All of the Records (with EOF) - Page 9
November 27, 2000
By building a table that displays the first five records of a
recordset, we get some idea of the techniques and practice with
the HTML codes for tables. However, most of the time you will
have no idea of how many records are in your recordset - so you
won't know how many times to repeat the loop. ADO provides a way
to test whether you are at the end of the records, and thus
should stop building rows. The property to use is called EOF
(which is short for 'End Of File'). The recordset object's EOF
property evaluates to True after you have moved beyond the last
row. Take a look at the following test:
Do while NOT oRSp.EOF
Response.Write oRSp("PeopleNameLast") & "<BR>"
oRSp.MoveNext
Loop
The above code is a little tricky to understand if you have never
used this construction before, so refill the coffee cup and
follow this closely:
- The way the
DO WHILE works, in general, is that before each
cycle of the loop ASP-ADO checks the test expression on the DO
WHILE line. If the test is True, ASP-ADO will perform the loop
again.
- But we are using the value of the
EOF property of oRSp as the
test object. rs.EOF is False when we are still in the data and
True when we are done with the records. That is the opposite of
what we want for the DO WHILE. We want to continue looping when
the rs.EOF is False (we are in middle of records) and we want to
stop looping when EOF is True (at end of records).
- VBScript provides us with the word
NOT to reverse the value
of the rs.EOF. Now when rs.EOF returns a False (in middle of
records) NOT changes that into a True and the loop is performed
again. When the rs.EOF is set to True (at end of records), NOT
turns that into a False and the looping stops.
Remember that every loop must have a way to end. In this case we
include the rs.MoveNext line to tell ADO's cursor to go to the
next record. Eventually ASP-ADO will move beyond the last record
and then the rs.EOF will turn to true. Our test will "NOT" that
True into a False and end the loop right there at the end of the
records.
Common Errors
- Leaving the "
RS." off of rs.EOF
- Leaving
RS.MoveNext out of the loop
- Leaving out the "
NOT" from the expression
- Putting
<TABLE> inside the loop
- Leaving out
</TABLE>.
Building a Table - Page 8
Beginning ASP Databases
Try It Out - Table Using EOF - Page 10
|