BOF, EOF 및 책갈피 속성 예제(VB)
이 예제에서는 사용자가 Recordset의 첫 번째 또는 마지막 레코드를 지나 이동하려고 하면 BOF 및 EOF 속성을 사용하여 메시지를 표시합니다. Bookmark 속성을 사용하여 사용자가 Recordset의 레코드에 플래그를 지정하고 나중에 레코드로 돌아갈 수 있도록 합니다.
'BeginBOFVB
'To integrate this code
'replace the data source and initial catalog values
'in the connection string
Public Sub Main()
On Error GoTo ErrorHandler
'recordset and connection variables
Dim Cnxn As ADODB.Connection
Dim rstPublishers As ADODB.Recordset
Dim strCnxn As String
Dim strSQLPubs As String
'record variables
Dim strMessage As String
Dim intCommand As Integer
Dim varBookmark As Variant
' open connection
Set Cnxn = New ADODB.Connection
strCnxn = "Provider='sqloledb';Data Source='MySqlServer';" & _
"Initial Catalog='Pubs';Integrated Security='SSPI';"
Cnxn.Open strCnxn
' Open recordset and use client cursor
' to enable AbsolutePosition property
Set rstPublishers = New ADODB.Recordset
strSQLPubs = "SELECT pub_id, pub_name FROM publishers ORDER BY pub_name"
rstPublishers.Open strSQLPubs, strCnxn, adUseClient, adOpenStatic, adCmdText
rstPublishers.MoveFirst
Do Until rstPublishers.EOF
' 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))
' Check user input
Select Case intCommand
Case 1
' Move forward trapping for EOF
rstPublishers.MoveNext
If rstPublishers.EOF Then
MsgBox "Moving past the last record." & _
vbCr & "Try again."
rstPublishers.MoveLast
End If
Case 2
' Move backward trapping for BOF
rstPublishers.MovePrevious
If rstPublishers.BOF Then
MsgBox "Moving past the first record." & _
vbCr & "Try again."
rstPublishers.MoveFirst
End If
Case 3
' Store the bookmark of the current record
varBookmark = rstPublishers.Bookmark
Case 4
' Go to the record indicated by the stored bookmark
If IsEmpty(varBookmark) Then
MsgBox "No Bookmark set!"
Else
rstPublishers.Bookmark = varBookmark
End If
Case Else
Exit Do
End Select
Loop
' clean up
rstPublishers.Close
Cnxn.Close
Set rstPublishers = Nothing
Set Cnxn = Nothing
Exit Sub
ErrorHandler:
' clean up
If Not rstPublishers Is Nothing Then
If rstPublishers.State = adStateOpen Then rstPublishers.Close
End If
Set rstPublishers = Nothing
If Not Cnxn Is Nothing Then
If Cnxn.State = adStateOpen Then Cnxn.Close
End If
Set Cnxn = Nothing
If Err <> 0 Then
MsgBox Err.Source & "-->" & Err.Description, , "Error"
End If
End Sub
'EndBOFVB
이 예제에서는 Bookmark 및 Filter 속성을 사용하여 Recordset의 제한된 보기를 만듭니다. 책갈피 배열에서 참조하는 레코드만 액세스할 수 있습니다.
Attribute VB_Name = "BOF"