?
Solved

Sort Recordset?

Posted on 2003-03-11
3
Medium Priority
?
237 Views
Last Modified: 2013-11-26
I have an ADODB.Recordset with some 50 records in, one of which includes name I would like to sort these into Alphabetical order before printing in word.  How do I do it?
0
Comment
Question by:amp81
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 1000 total points
ID: 8110225
Hi amp81,

It is always more efficient to sort the recordset when you generate it, using an Order by clause in your sql statement for example.

If you cannot do this then you can use the Recordset's .Sort method as shown below:

Sub SortRecordset(strDBPath As String, _
                  strTable As String, _
                  strSort As String, _
                  strDisplayField As String)
   Dim cnn As ADODB.Connection
   Dim rst As ADODB.Recordset
   
   ' Open the connection.
   Set cnn = New ADODB.Connection
   With cnn
      .Provider = "Microsoft.Jet.OLEDB.4.0"
      .Open strDBPath
   End With
   
   Set rst = New ADODB.Recordset
   With rst
      ' Specify client-side cursor.
      .CursorLocation = adUseClient

      ' Open the table by using a scrolling Recordset object.
      .Open Source:= strTable, _
            ActiveConnection:= cnn, _
            CursorType:=adOpenKeyset, _
            LockType:=adLockOptimistic

      ' Sort the Recordset object.
      .Sort = strSort

      ' Print the records.
      Do While Not .EOF
         Debug.Print .Fields(strDisplayField).Value
         .MoveNext
      Loop

      ' Close the Recordset object.
      .Close
End With

   ' Close connection and destroy object variables.
   cnn.Close
   Set rst = Nothing
   Set cnn = Nothing
End Sub

For example, to use this procedure to sort a Recordset object opened on the Customers table in the Northwind database by the Country and Region fields and display the values in the CustomerID field, you can use a line of code like this:

SortRecordset "c:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb", _
   "Customers","Country, Region", "CustomerID"

The important part of this is to specify adUseClient as sorting will not work if you are using a server side cursor with access. With SQL server or another provider it may be different.

Tim Cottee MCSD, MCDBA, CPIM
http://www.timcottee.tk 

Brainbench MVP for Visual Basic
http://www.brainbench.com
0
 
LVL 1

Author Comment

by:amp81
ID: 8110288
Thanks for help working fine now.
0
 

Expert Comment

by:archekrypt
ID: 8151787
DON'T USE ADODC!!!!!!

Use ADO in CODE and open dbs as SQL statements, MUCH faster and MUCH more reliable...
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question