Sort Property Example (VB)

Microsoft ActiveX Data Objects (ADO)

Sort Property Example (VB)

This example uses the Recordset object's Sort property to reorder the rows of a Recordset derived from the Authors table of the Pubs database. A secondary utility routine prints each row.

Sub Main()
    SortX
End Sub

Public Sub SortX()
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset

rst.CursorLocation = adUseClient
cnn.Open "DSN=Pubs;Provider=MSDASQL;uid=sa;pwd=;"
rst.Open "SELECT * FROM Authors", cnn, _
            adOpenStatic, adLockReadOnly, adCmdText
SortXprint "Initial Order", rst

rst.Sort = "au_lname ASC, au_fname ASC"
SortXprint "Last Name Ascending", rst

rst.Sort = "au_lname DESC, au_fname ASC"
SortXprint "Last Name Descending", rst

rst.Close
cnn.Close
End Sub

This is the secondary utility routine that prints the given title, and the contents of the specified Recordset.

Public Sub SortXprint ( title As String, rstp As ADODB.Recordset )
Debug.Print "---------------" & title & "---------------" 
Debug.Print "First Name  Last Name" & vbCr & _
                "---------------------------------------------------"
rstp.MoveFirst
While Not rstp.EOF
    Debug.Print rstp!au_fname & " " & rstp!au_lname 
    rstp.MoveNext
Wend
Debug.Print 
End Sub