Database
  Home arrow Database arrow Page 2 - Manipulating ADO Recordsets
Iron Speed
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Download TestComplete 
Windows Web Hosting
 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DATABASE

Manipulating ADO Recordsets
By: Jayaram Krishnaswamy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 6
    2006-06-21

    Table of Contents:
  • Manipulating ADO Recordsets
  • Sorting the recordset
  • Filtering the Recordset
  • Using the Filter property of the recordset
  • Recordset's Find() method

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    Iron Speed
     
    ADVERTISEMENT

    Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Manipulating ADO Recordsets - Sorting the recordset
    (Page 2 of 5 )

    Sorting is almost an unconscious activity in daily life. We try to put things in compartments. In ADO you often sort as well. A familiar sorted list is the names of the states that you often come across when entering your address information on the Internet. If it is not sorted, you are at a loss to choose yours easily. On the other hand if it is alphabetically sorted, you just scroll down until you find yours.

    In a recordset you may want to sort on any column name, or a set of column names in a chosen order. For example you may want to sort by "first name" and then you may want sort by the "last name" in the list that was sorted. When you call this property, a temporary index is created for each column specified, and the list is ordered efficiently. If the field is already indexed, it is that much more efficient since the engine does not have to create one. However for this to work you need to open the recordset after setting the CursorLocation to adUseClient. This is because  Microsoft Jet 4.0 OleDB provider does not support the IViewSort Interface needed by ADO to do the sorting.

    Also, if you want to revert to the original unsorted list, you may provide an empty string for the Sort property as you will see in the example. Of course it goes without saying that you should avoid using the keywords for field names. The syntax for this property usage is:

    RecordSet.Sort  "fieldName1 asc (or desc), fieldName2 asc
    (or desc),.."

    In order to illustrate the "Sort" property, create a new form in design and add a couple of controls as shown. Some cosmetic changes have been made, and the RowSourceType of the list has been set to "ValueList" as we will be assigning the return value string to this property. If this is left  as the default, Table/Query, then you will not see any display because the string is neither a table nor a query. 

    Basically a recordset is retrieved from the database using the code that we previously discussed in other tutorials. Each of the command buttons (Unsorted, Sorted: Last Name Ascending, Sorted: LastName Ascending and Hire Date descending) can sort the list (or revert to the original in one case) using the code in their click events as shown in the snippet that follows the picture.

    Option Compare Database
    Private cn As ADODB.Connection
    Private rst As ADODB.Recordset
    Private Sub Command0_Click()
    'don't sort the recordset
    rst.Sort = ""
    Call poplist
    End Sub
    Private Sub Command1_Click()
    'Sort the recordset with Last Name ascending
    rst.Sort = "LastName asc"
    Call poplist
    End Sub
    Private Sub Command2_Click()
    'sort with Last Name ascending and Hire Date descending
    rst.Sort = "LastName asc, HireDate desc"
    Call poplist
    End Sub
    Private Sub Form_Load()
    Dim str As String
    str = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=C:Documents and SettingsJay _
    My DocumentsRetrieve.mdb;" & _ "Persist Security Info=False" Dim strSql As String 'strSql = "Employees" Set cn = New ADODB.Connection cn.Open str Set rst = New ADODB.Recordset rst.CursorLocation = adUseClient strSql = "Select LastName, FirstName, City,
    HireDate from Employees" rst.Open strSql, cn, adOpenStatic MsgBox (rst.Fields.Count) Set Me.Recordset = rst End Sub Sub poplist() Dim strg strg = "" While Not rst.EOF strg = strg & rst.Fields.Item(0).Value & ":" & _
    rst.Fields.Item(1).Value & ":" & _ rst.Fields.Item(2).Value & ":" & _ "rst.Fields.Item(3).Value & ";" rst.MoveNext Wend Me.List11.RowSource = strg End Sub

    The recordset opens with the Form's Load() event. Observe that the recordset is opened after setting the CursorLocation property to adUseClient. Some of the message boxes in this and other snippets are left in place, so that you can see what is happening. Command0's click will revert the record set to its original state. Clicking Command1 will sort the list so that the LastName will be in ascending order (it is actually the default). Command2 button's click will not only sort by LastName, it will also sort by HireDate (see Buchanan's information). The next picture shows what it looks like when the list is sorted by LastName in ascending order. Each of the click events also call the procedure poplist(), which populates the ListBox.

    This next picture shows sorting by LastName ascending and HireDate descending.

    More Database Articles
    More By Jayaram Krishnaswamy


       · Getting information out of a database without the help of Sorting, Filtering, and...
     

    DATABASE ARTICLES

    - Database Programming in C# with MySQL : Usin...
    - Formatting Techniques for Data Access from E...
    - Data Access from Excel VBA
    - Generating a Multiple Table Crystal Report u...
    - ADO and the Command Object
    - On Wiring Up an ADO Data Control
    - Reading and Writing to Files on the Intranet
    - Using ADO Record to Create and Navigate Intr...
    - Using Data Access Pages to Access Data on a ...
    - Using ADO with the SQL Native Client
    - ADO`s Stream Object
    - Opening a Record Object Referencing an Open ...
    - Introducing Jasper (SQL Anywhere 10 Beta)
    - Creating a Database Project in VS 2005
    - Manipulating ADO Recordsets




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway