Can someone show me an example of using the DAO recordset object to sort DESCENDING?
Thanks,
Thai
Printable View
Can someone show me an example of using the DAO recordset object to sort DESCENDING?
Thanks,
Thai
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):
Hope this helps.Code:StrSQL = "Select * from titles where title like 'a*' ORDER BY [sort_field_name] DESC;"
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
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.
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
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...
Your remaining task will be to Data1.Refresh appropriately...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
Thanks for the help!
Thai
How abt my solution? does it what you looking for?
Thread