HandsOn Sorting Records

1. In the Code window, enter the SortRecords procedure as shown below.

Sub SortRecords()

Dim conn As ADODB.Connection Dim rst As ADODB.Recordset

Set conn = New ADODB.Connection

Creating and Manipulating Databases with ADO

conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=" & CurrentProject.Path & _ "\Northwind.mdb"

Set rst = New ADODB.Recordset

' sort on non-indexed field With rst

.CursorLocation = adUseClient

.Open "Customers", conn, adOpenKeyset, adLockOptimistic .Sort = "Country" Do Until rst.EOF

Debug.Print rst.Fields("CompanyName").Value & ": " &_

rst.Fields("Country").Value .MoveNext Loop

Debug.Print rst.Fields("CompanyName").Value & ": " &_

rst.Fields("Country").Value .MoveNext Loop .Close End With

Set rst = Nothing conn.Close Set conn = Nothing End Sub

Notice that after sorting records in the specified order, the Sort property is set to an empty string and records are displayed in the order in which they physically appear in the table.

0 0

Post a comment