Sun
ONE Active Server Pages Product Home Page Developer Site Version
 

ContentsPreviousNextIndex



ADO Recordset Object BOF, EOF Properties

BOF indicates that the current record position is before the first record in a Recordset object.

EOF indicates that the current record position is after the last record in a Recordset object.

BOF, EOF Properties Return Values

The BOF and EOF properties return Boolean values.

BOF, EOF Properties Remarks

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 when you move from record to record.

The BOF property returns True (-1) if the current record position is before the first record and False (0) 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 if the current record position is on or before the last 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 object's RecordCount property setting is zero. 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.

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.

This table shows which ADO Recordset Object Move Method methods are allowed with different combinations of the BOF and EOF properties:

MoveFirst
MoveLast
Move Previous Move < 0
Move 0
Move Next Move > 0
BOF = True,
EOF = False
Allowed
Error
Error
Allowed
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 guarantee that the method will successfully locate a record; it only means that calling the specified Move method won't generate an error.

The following table shows what happens to the BOF and EOF property settings when you call various Move methods but are unable to successfully locate a record.

BOF
EOF
MoveFirst, MoveLast
Set to True
Set to True
Move 0
No change
No change
MovePrevious, Move < 0
Set to True
No change
MoveNext, Move > 0
No change
Set to True

BOF, EOF Properties Example

This Visual Basic example uses the BOF and EOF properties to display a message if a user tries to move past the first or last record of a recordset. It uses the ADO Recordset Object Bookmark Property to let the user flag a record in a recordset and return to it later.

Public Sub BOFX() 
Dim rstPublishers As ADODB.Recordset 
Dim strCnn As String 
Dim strMessage As String 
Dim intCommand As Integer 
Dim varBookmark As Variant
` Open recordset with data from Publishers table. 
strCnn = "driver={SQL Server};server=srv;" & _ 
"uid=sa;pwd=;database=pubs" 
Set rstPublishers = New ADODB.Recordset 
rstPublishers.CursorType = adOpenStatic 
` Use client cursor to enable AbsolutePosition property. 
rstPublishers.CursorLocation = adUseClient 
rstPublishers.Open "SELECT pub_id, pub_name FROM publishers " & _ 
"ORDER BY pub_name", strCnn, , , adCmdText 
rstPublishers.MoveFirst 
Do While True 
` Display information about current record 
` and get user input. 
strMessage = "Publisher: " & rstPublishers!pub_name & _ 
vbCr & "(record " & rstPublishers.AbsolutePosition & _ 
" of " & rstPublishers.RecordCount & ")" & vbCr & vbCr & _ 
"Enter command:" & vbCr & _ 
"[1 - next / 2 - previous /" & vbCr & _ 
"3 - set bookmark / 4 - go to bookmark]" 
intCommand = Val(InputBox(strMessage))
Select Case intCommand 
` Move forward or backward, trapping for BOF 
` or EOF. 
Case 1 
rstPublishers.MoveNext 
If rstPublishers.EOF Then 
MsgBox "Moving past the last record." & _ 
vbCr & "Try again." 
rstPublishers.MoveLast 
End If 
Case 2 
rstPublishers.MovePrevious 
If rstPublishers.BOF Then 
MsgBox "Moving past the first record." &
_vbCr & "Try again." 
rstPublishers.MoveFirst 
End If
` Store the bookmark of the current record. 
Case 3 
varBookmark = rstPublishers.Bookmark
` Go to the record indicated by the stored 
` bookmark. 
Case 4 
If IsEmpty(varBookmark) Then 
MsgBox "No Bookmark set!" 
Else 
rstPublishers.Bookmark = varBookmark 
End If 
Case Else 
Exit Do 
End Select 
Loop 
rstPublishers.Close 
End Sub


ContentsPreviousNextIndex