If you move beyond the boundaries of a recordset, an error will occur. To avoid this rather unpleasant side effect of poor programming practice, you should test to see whether you have reached the beginning or end of the recordset. Make sense?
Before using MoveNext or MoveFirst, you should check the value of BOF and EOF.
If Not rst.BOF Then rst.MovePrevious or
If Not rst.EOF Then rst.MoveNext To help you understand the behavior of these properties, consider the following scenarios:
□ If you issue MoveNext while the cursor is on the last row, EOF returns True. If you then issue MoveNext again, EOF remains True and an error occurs.
□ If you issue MovePrevious while the cursor is on the first row, BOF returns True. If you then issue MovePrevious again, BOF remains True and an error occurs.
□ As mentioned earlier, I use AbsolutePosition to test for an empty recordset, but as we've discovered, AbsolutePosition can't be used on ForwardOnly or table-type recordsets, so we need another method for determining whether a recordset contains any records.
□ BOF and EOF are widely used when looping through recordsets, when you don't know how many records have been returned. Usually, row processing begins at the first row, and continues unil all the rows have been processed. Sometimes, however, processing begins at the last record, and continues backwards until the beginning of the recordset. BOF and EOF allow you to do this.
For example, the following code shows a standard forward looping construct:
Set rst = dbs.OpenRecordset("SELECT * FROM Table1", dbOpenDynaset) Do While Not rst.EOF 'Process the rows rst.MoveNext
The following example demonstrates a typical reverse-direction loop:
Set rst = dbs.OpenRecordset("SELECT * FROM Table1", dbOpenDynaset) rst.MoveLast Do While Not rst.BOF 'Process the rows rst.MovePrevious
Was this article helpful?