Results 1 to 8 of 8

Thread: dao sorting

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Posts
    168
    Can someone show me an example of using the DAO recordset object to sort DESCENDING?

    Thanks,
    Thai

  2. #2
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Leavenworth KS USA
    Posts
    482
    Since you don't say how you're setting up DAO, I'll start with a MS example at
    http://support.microsoft.com/support.../Q147/8/82.asp

    Assuming you follow from the example, then to sort the resultset use something like this (note the bold text):

    Code:
    StrSQL = "Select * from titles where title like 'a*' ORDER BY [sort_field_name] DESC;"
    Hope this helps.



  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Posts
    168
    Sorry I should have been more clear... I need an example of how to sort the recordset in descending order using the dao control's sort and its attributes. Is there a way to sort descending with the control not using SQL? I read about it on MS's web site but they gave no examples, they just said it was possible. Please help if you can..

    Thanks,
    Thai

  4. #4
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Leavenworth KS USA
    Posts
    482
    I'm a bit of an SQL-bonehead-snob, so I tend to think it's mo' betta to create a Recordset with an SQL statement. Tell me which control you're using, and I'll try to help.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Posts
    168
    k.. I am using two controsl.. the db grid and the dao control. I set the property of the data and recordset in the dao control so it has the recordset.. then i set the db grid's datasource to Data1 (which is the dao control).

    So now I can manipulate the recordset in code by using data1.recordset yadda yadda and the changes will appear in the grid. How do I make Data1 sort DESCENDING on a field I want? It should automatically show the changes in the grid when the sort happens. That is my goal Thanks

    Thai

  6. #6
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Leavenworth KS USA
    Posts
    482
    otay, if I understand you correctly (which is a big "IF" as I'm being thick-skulled tonight), this example is stolen directly from the DAO 3.5 help file, with a minor DESC modification...

    Code:
    This example demonstrates the Sort property by changing its value and creating a new Recordset. The SortOutput function is required for this procedure to run.
    
    Sub SortX()
    
    	Dim dbsNorthwind As Database
    	Dim rstEmployees As Recordset
    	Dim rstSortEmployees As Recordset
    
    	Set dbsNorthwind = OpenDatabase("Northwind.mdb")
    	Set rstEmployees = _
    		dbsNorthwind.OpenRecordset("Employees", dbOpenDynaset)
    
    	With rstEmployees
    		SortOutput "Original Recordset:", rstEmployees
                    ' "magic" happens here...
    		.Sort = "LastName DESC, FirstName"
    		' Print report showing Sort property and record order.
    		SortOutput "Recordset after changing Sort property:", rstEmployees
    		' Open new Recordset from current one.
    		Set rstSortEmployees = .OpenRecordset
    		' Print report showing Sort property and record order.
    		SortOutput "New Recordset:", rstSortEmployees
    		rstSortEmployees.Close
    		.Close
    	End With
    
    	dbsNorthwind.Close
    
    End Sub
    
    Function SortOutput(strTemp As String, _
    	rstTemp As Recordset)
    
    	With rstTemp
    		Debug.Print strTemp
    		Debug.Print "    Sort = " & _
    			IIf(.Sort <> "", .Sort, "[Empty]")
                    .MoveFirst
    
    		' Enumerate Recordset.
    		Do While Not .EOF
    			Debug.Print "        " & !LastName & ", " & !FirstName
    			.MoveNext
    		Loop
    	End With
    End Function
    Your remaining task will be to Data1.Refresh appropriately...

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Mar 2000
    Posts
    168
    Thanks for the help!

    Thai

  8. #8
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Lightbulb Clone recordset

    How abt my solution? does it what you looking for?
    Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width