Sorting Records : Recordset Sort « Access « VBA / Excel / Access / Word






Sorting Records

 
Sub SortRecords()
   Dim conn As ADODB.Connection
   Dim myRecordset As ADODB.Recordset

   Set conn = New ADODB.Connection
   conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & CurrentProject.Path & "\mydb.mdb"
   Set myRecordset = New ADODB.Recordset

   With myRecordset
      .CursorLocation = adUseClient
      .Open "Customers", conn, adOpenKeyset, adLockOptimistic
      .Sort = "Country"
      Do Until myRecordset.EOF
         Debug.Print myRecordset.Fields("CompanyName").Value & ": " & myRecordset.Fields("Country").Value
         .MoveNext
      Loop

      .Sort = ""
      Do Until .EOF
         Debug.Print myRecordset.Fields("CompanyName").Value & ": " & myRecordset.Fields("Country").Value
         .MoveNext
      Loop
      .Close
   End With

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

 








Related examples in the same category