| << 13.3.0- Using the Recordset Object | Chapter13 | 13.3.2- Moving Through Records >> |
The BOF and EOF Properties
As we've already seen, we view the data contained in a record by first moving the recordset's cursor so that it's pointing at that record. But there are only a finite number of records in the recordset – we need some way of knowing when we've pushed the cursor past the last record, or past the first one.
There are two properties whose values provide that information – they're called BOF (beginning-of-file) and EOF (end-of-file). Both are Boolean values, so each holds a value True or False which reflects the current position of the cursor.
To understand, let's consider a recordset that contains four records. The following diagram shows the same recordset six times – the four boxes represent the four records and the gray rectangles represent the cursor stepping through the records of the recordset. Notice that, in addition to the four records in our recordset, there are two additional 'positions' (one at the beginning and one at the end). They aren't records – they're just imaginary 'markers' that represent the extremes of the recordset:
As we step through the recordset, we see that:
- When the cursor is pointing to a valid record (as in the middle four instances), then BOF and EOF are both False (because the cursor is neither at the beginning or the end of the recordset).
- If we move the cursor so far backwards through the recordset that we go past the first record (as in the left-most instance), then the cursor lands on the 'marker' preceding the first record. Then, the value of BOF changes to True
- Alternatively, if we move the cursor so far forwards through the recordset that we go past the last record (as in the right-most instance), then the cursor lands on the 'marker' after the last record. Then, the value of EOF changes to True
We've seen already that this can be very useful. We've already used the EOF property as the control that allows us to step through every record and to stop as soon as we move past the last one:
While Not objRS.EOF ' now loop through the records
Response.Write objRS.Fields("Title") & ", "
objRS.MoveNext
Wend
The BOF and EOF properties work just like this, provided there's at least one record in the recordset. If there are no records in the recordset, then the 'beginning' and 'end' markers coincide – and therefore the values BOF and EOF are simultaneously True. (Conversely, if you have a recordset in which BOF and EOF are simultaneously True, then the recordset has no records in it!)
| << 13.3.0- Using the Recordset Object | Chapter13 | 13.3.2- Moving Through Records >> |

RSS

