>
BOF, EOF Properties
Applies To
Dynaset-Type Recordset Object, Recordset Object, Snapshot-Type Recordset Object, Table-Type Recordset Object.
BOF — returns a value that indicates whether the current record position is before the first record in a Recordset object.EOF — returns a value that indicates whether the current record position is after the last record in a Recordset object.
Return Values
The BOF property returns True (-1) if the current record position is before the first record, and False if the current record position is on or after the first record.
The EOF property returns True if the current record position is after the last record, and False (0) if the current record position is on or before the last record.
Remarks
The BOF and EOF properties return values are determined by the location of the current record pointer.
You can use the BOF and EOF properties to determine whether a Recordset object contains records or whether you've gone beyond the limits of a Recordset object as you move from record to record.
If either the BOF or EOF property is True, there is no current record.
If you open a Recordset object containing no records, the BOF and EOF properties are set to True, and the recordset's RecordCount property setting is 0. When you open a Recordset object that contains at least one record, the first record is the current record and the BOF and EOF properties are False; they remain False until you move beyond the beginning or end of the Recordset object using the MovePrevious or MoveNext method, respectively. When you move beyond the beginning or end of the recordset, there is no current record or no record exists.
If you delete the last remaining record in the Recordset object, the BOF and EOF properties may remain False until you attempt to reposition the current record.
If you use the MoveLast method on a Recordset object containing records, the last record becomes the current record; if you then use the MoveNext method, the current record becomes invalid and the EOF property is set to True. Conversely, if you use the MoveFirst method on a Recordset object containing records, the first record becomes the current record; if you then use the MovePrevious method, there is no current record and the BOF property is set to True.
Typically, when you work with all the records in a Recordset object, your code will loop through the records using the MoveNext method until the EOF property is set to True.
If you use the MoveNext method while the EOF property is set to True or the MovePrevious method while the BOF property is set to True, an error occurs.
This table shows which Move methods are allowed with different combinations of the BOF and EOF properties.
| MoveFirst, MoveLast | MovePrevious, Move < 0 | Move 0 | MoveNext, Move > 0 |
|
BOF=True, EOF=False | Allowed | Error | Error | Allowed |
| MoveFirst, MoveLast | MovePrevious, Move < 0 | Move 0 | MoveNext, Move > 0 |
| | | | |
BOF=False, EOF=True | Allowed | Allowed | Error | Error |
Both True | Error | Error | Error | Error |
Both False | Allowed | Allowed | Allowed | Allowed |
Allowing a Move method doesn't mean that the method will successfully locate a record. It merely indicates that an attempt to perform the specified Move method is allowed and won't generate an error. The state of the BOF and EOF flags may change as a result of the attempted Move.
Effects of specific methods on the BOF and EOF property settings are as follow:
An OpenRecordset method internally invokes a MoveFirst method. Therefore, an OpenRecordset method on an empty set of records results in the BOF and EOF properties being set to True. (See the following table for the behavior of a failed MoveFirst method.)All Move methods that successfully locate a record clear (set to False) both the BOF and EOF properties.An AddNew method followed by an Update method that successfully inserts a new record will clear the BOF property, but only if the EOF property is already set to True. The state of the EOF property will always remain unchanged. As defined by the Microsoft Jet database engine, the current record pointer of an empty recordset is at the end of a file, so any new record is inserted after the current record.Any Delete method, even if it removes the only remaining record from a recordset, won't change the setting of the BOF or EOF property.
The effect of Move methods that don't locate a record on the BOF and EOF property settings is shown in the following table.
| BOF | EOF |
|
MoveFirst, MoveLast | True | True |
Move 0 | No change | No change |
MovePrevious, Move < 0 | True | No change |
MoveNext, Move > 0 | No change | True |
See Also
MoveFirst, MoveLast, MoveNext, MovePrevious Methods.
Example
This example uses the BOF and EOF properties to find the beginning and end of an Orders table. The example moves through the records from the beginning of the file to the end and then from the end to the beginning. Notice that there is no current record immediately following the first loop.
Set dbsNorthwind = DBEngine.Workspaces(0).OpenDatabase("Northwind.mdb")
Set rstOrders = dbsNorthwind.OpenRecordset("Orders") ' Open Recordset.
Do Until rstOrders.EOF ' Do until end of file.
rstOrders.MoveNext ' Move to next record.
...
Loop
rstOrders.MoveLast ' Establish a current record.
Do Until rstOrders.BOF ' Do until beginning of file.
rstOrders.MovePrevious ' Move to previous record.
...
Loop
rstOrders.Close
dbsNorthwind.Close
Example (Microsoft Access)
The following example uses the EOF property of a Recordset object to move the current record pointer past the last record in the Recordset object, then uses the BOF property to move the current record pointer to before the first record in the Recordset object.
Sub EndsOfFile()
Dim dbs As Database, rst As Recordset
' Return Database variable that points to current database.
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("Orders")
' Do until end of file.
Do Until rst.EOF
' Move to next record.
rst.MoveNext
.
.
.
Loop
' Move to last record to set a current record.
rst.MoveLast
' Do until beginning of file.
Do Until rst.BOF
rst.MovePrevious
.
.
.
Loop
rst.Close
dbs.Close
End Sub