ADO - EOF&BOF

In ADO (ActiveX Data Objects), the BOF and EOF properties are used to determine whether the current record position in a Recordset object is before the first record or after the last record.


1. BOF (Beginning of File)

  • Meaning: Indicates whether the current record position is before the first record in the Recordset.

  • Syntax:

    If rs.BOF Then
        ' Code to handle when before first record
    End If
    
  • When True:

    • When the Recordset is empty (no records at all).

    • When you move before the first record, for example, using:

      rs.MovePrevious
      

      while already on the first record.


2. EOF (End of File)

  • Meaning: Indicates whether the current record position is after the last record in the Recordset.

  • Syntax:

    If rs.EOF Then
        ' Code to handle when after last record
    End If
    
  • When True:

    • When the Recordset is empty.

    • When you move past the last record, for example, using:

      rs.MoveNext
      

      while already on the last record.


3. Common Usage Example

In ADO, it’s common to check for both properties before processing records:

Set rs = conn.Execute("SELECT * FROM Employees")

If rs.BOF And rs.EOF Then
    MsgBox "No records found!"
Else
    rs.MoveFirst
    Do Until rs.EOF
        MsgBox rs("EmployeeName")
        rs.MoveNext
    Loop
End If

4. Key Points

  • BOF and EOF are Boolean properties (True or False).

  • Both are True when the Recordset is empty.

  • You typically check If rs.EOF in loops to detect when you’ve reached the end of records.

  • Moving before the first or after the last record sets these flags to True.