MoveFirst, MoveLast, MoveNext, dan MovePrevious Methods Example (VB)

Contoh ini menggunakan metode MoveFirst, MoveLast, MoveNext, dan MovePrevious untuk memindahkan penunjuk rekaman Recordset berdasarkan perintah yang disediakan. Prosedur MoveAny diperlukan agar prosedur ini berjalan.

    'To integrate this code  
    'replace the data source and initial catalog values  
    'in the connection string  
Public Sub Main()  
    On Error GoTo ErrorHandler  
    ' connection and recordset variables  
    Dim rstAuthors As ADODB.Recordset  
    Dim Cnxn As ADODB.Connection  
    Dim strCnxn As String  
    Dim strSQLAuthors  
     ' record variables  
    Dim strMessage As String  
    Dim intCommand As Integer  
    ' Open connection  
    Set Cnxn = New ADODB.Connection  
    strCnxn = "Provider='sqloledb';Data Source='MySqlServer';" & _  
        "Initial Catalog='Pubs';Integrated Security='SSPI';"  
    Cnxn.Open strCnxn  
    ' Open recordset from Authors table  
    Set rstAuthors = New ADODB.Recordset  
    rstAuthors.CursorLocation = adUseClient  
    ' Use client cursor to enable AbsolutePosition property  
    strSQLAuthors = "Authors"  
    rstAuthors.Open strSQLAuthors, Cnxn, adOpenStatic, adLockReadOnly, adCmdTable  
    ' Show current record information and get user's method choice  
        strMessage = "Name: " & rstAuthors!au_fname & " " & _  
            rstAuthors!au_lname & vbCr & "Record " & _  
            rstAuthors.AbsolutePosition & " of " & _  
            rstAuthors.RecordCount & vbCr & vbCr & _  
            "[1 - MoveFirst, 2 - MoveLast, " & vbCr & _  
            "3 - MoveNext, 4 - MovePrevious]"  
        intCommand = Val(Left(InputBox(strMessage), 1))  
         ' for exiting the loop  
        If intCommand < 1 Or intCommand > 4 Then  
            MsgBox "You either entered a non-number or canceled the input box. Exit the application."  
            Exit Do  
        End If  
        ' Use specified method while trapping for BOF and EOF  
        Select Case intCommand  
            Case 1  
            Case 2  
            Case 3  
                If rstAuthors.EOF Then  
                    MsgBox "Already at end of recordset!"  
                End If  
            Case 4  
                If rstAuthors.BOF Then  
                    MsgBox "Already at beginning of recordset!"  
                End If  
        End Select  
    ' clean up  
    Set rstAuthors = Nothing  
    Set Cnxn = Nothing  
    Exit Sub  
    ' clean up  
    If Not rstAuthors Is Nothing Then  
        If rstAuthors.State = adStateOpen Then rstAuthors.Close  
    End If  
    Set rstAuthors = 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  

Lihat juga

MoveFirst, MoveLast, MoveNext, dan MovePrevious Methods (ADO)
Objek Recordset (ADO)