473,385 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Sorting a recordset by fields in Access (ADO)

Hello All,
I am having problems with sorting a recordset by fields in Access. I
have a table with three columns called ID, SNo and Time and would like
to sort the records by Time. I would like to sort them in the actual
database and not just Order them with SQL. I have included the code I
wrote below. Please bear in mind that I am very new to Access and SQL.
I used the Helpfile of Access for this code but i can't make it work
for me.
Regards,
Alex

‘////////////////////////////////////////
Sub SortFields()
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset

rst.CursorLocation = adUseClient
Set cnn = CurrentProject.Connection

rst.Open "SELECT Table1.ID, Table1.SNo, Table1.Time FROM Table1", cnn,
_
adOpenStatic, adLockReadOnly, adCmdText

rst.Sort = "Time DESC"

rst.UpdateBatch
rst.Close
cnn.Close
End Sub
‘/////////////////////////////////////////
Nov 13 '05 #1
3 35401
You can use Order By to control the order in which the records are returned,
and you can use rst.Sort to sort the records in memory after the records are
returned. In a relational database, by definition, there is no intrinsic
order of records in a table. You are supposed to look at it as a bag of
unsorted records that you can return in any order you like.

On 26 Oct 2004 00:42:10 -0700, pe******@hotmail.com (alex_peri) wrote:
Hello All,
I am having problems with sorting a recordset by fields in Access. I
have a table with three columns called ID, SNo and Time and would like
to sort the records by Time. I would like to sort them in the actual
database and not just Order them with SQL. I have included the code I
wrote below. Please bear in mind that I am very new to Access and SQL.
I used the Helpfile of Access for this code but i can't make it work
for me.
Regards,
Alex

‘////////////////////////////////////////
Sub SortFields()
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset

rst.CursorLocation = adUseClient
Set cnn = CurrentProject.Connection

rst.Open "SELECT Table1.ID, Table1.SNo, Table1.Time FROM Table1", cnn,
_
adOpenStatic, adLockReadOnly, adCmdText

rst.Sort = "Time DESC"

rst.UpdateBatch
rst.Close
cnn.Close
End Sub
‘/////////////////////////////////////////


Nov 13 '05 #2
You must open the proper type of recordset. You have specified a recordset
of type 'readonly', and then tried to update it, which you cannot do.

In the following example, after creating the recordset, I set the Source
property of the recordset first, for clarity. You can also place it in the
recordset's open event, as you have done, but it can get messy if you have a
very long SQL string.
-------------------------------------------------
Dim cnxn As ADODB.Connection
Dim rs As ADODB.Recordset
Set cnxn = CurrentProject.Connection
Set rs = New ADODB.Recordset
rs.Source = "SELECT rev_login from reviewer WHERE rev_login = " &
S2SQL(oldLogin)
rs.Open , cnxn, adOpenKeyset, adLockOptimistic, adCmdText
If Not .EOF Then
rs.Fields(0) = Me.txtLogin
rs.Update
rs.Close
End If
Set rs = Nothing
-------------------------------------------------
The ADO Open event has five necessary parts:

1. The source, which in my example case, I've already supplied separately.
This is usually a SQL string or a table.

2. The active connection, which I called cnxn, and you have as cnn

3. The Cursor Type, one of:
Dynamic, Keyset, Static, or ForwardOnly (all prefixed with 'adOpen').
So far, I have not had any reason to use Dynamic or Static. While a
ForwardOnly cursor is the fastest, it is limited to one trip through the
recordset, 'forward only'. This is just what you need sometimes, but at
other times, use the adOpenKeyset. The performance is the best, and you
have freedom to do anything with the recordset you need.

4. The Lock Type, one of:
BatchOptimistic, Optimistic, Pessimistic, or ReadOnly (all prefixed with
'adLock'). I would recommend that you stick to Optimistic and ReadOnly, and
in fact, usually use Keyset and Optimistic as a pair, and ForwardOnly and
ReadOnly as a pair. If you want to be able to move any direction in a
recordset, and make changes to the data, use adOpenKeyset, with
adLockOptimistic. If you just need to read the data in a recordset as
quickly as possible, for a single or multiple records, use
adOpenForwardOnly, with adLockReadOnly. This will do for 99% of what you
are doing. One problem below is that you have used the line:

rst.UpdateBatch

First, you can't update a ReadOnly recordset. Second, you would only use

rst.Update

Third, as mentioned in Stevespost, you are better off to simply sort the
records in the SQL, as in:

rst.Open "SELECT Table1.ID, Table1.SNo, Table1.Time FROM Table1 ORDER BY
Time DESC"

5. The type of command - ususally adCmdText, for SQL queries, or
adCmdTableDirect, for fast access to a table.

Last, since you are using CurrentProject.Connection, there is no need for
the line:

cnn.Close
Darryl Kerkeslager
"alex_peri" <pe******@hotmail.com> wrote:
I am having problems with sorting a recordset by fields in Access. I
have a table with three columns called ID, SNo and Time and would like
to sort the records by Time. I would like to sort them in the actual
database and not just Order them with SQL. I have included the code I
wrote below. Please bear in mind that I am very new to Access and SQL.
I used the Helpfile of Access for this code but i can't make it work
for me.
Regards,
Alex

'////////////////////////////////////////
Sub SortFields()
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset

rst.CursorLocation = adUseClient
Set cnn = CurrentProject.Connection

rst.Open "SELECT Table1.ID, Table1.SNo, Table1.Time FROM Table1", cnn,
_
adOpenStatic, adLockReadOnly, adCmdText

rst.Sort = "Time DESC"

rst.UpdateBatch
rst.Close
cnn.Close
End Sub
'/////////////////////////////////////////

Nov 13 '05 #3
I saw an error and use of an undefined function, and this just cleans up my
example:
Dim cnxn As ADODB.Connection
Dim rs As ADODB.Recordset
Set cnxn = CurrentProject.Connection
Set rs = New ADODB.Recordset
rs.Source = "SELECT rev_login from reviewer WHERE rev_login = ' " & oldLogin
& " ' "
rs.Open , cnxn, adOpenKeyset, adLockOptimistic, adCmdText
If Not rs.EOF Then
rs.Fields(0) = Me.txtLogin
rs.Update
rs.Close
End If
Set rs = Nothing
Darryl Kerkeslager
Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tom Paquette | last post by:
Hi, Having made very good use of the ADO recordset Field indexes created by setting the Optimize property, I am thrashing through ADO.Net literature to identify the same functionality. Being...
1
by: Anand | last post by:
Hi i am having trouble adding a recordset into the access database, the code seems to be working fine it passs and parses through all variables just fine without showing any errors and also when i...
1
by: Boris Wilhelms | last post by:
Hello all, at first, sorry for my bad English, I’ll give my best  We have a strange problem reading Text- and VarChar-Fields. Our configuration: -Windows 2003 Server -MySQL Server 3.23.36...
1
by: Guinness Mann | last post by:
Greetings, I'm working in VS2003.NET using C#. I need to download a few rows from an SQL Server database table and then be able to page back and forth through them. I don't need to make any...
1
by: (Pete Cresswell) | last post by:
A stored procedure is returning three recordsets via ADO. Two of the recordsets have columns with names like "Estimate1", "Estimate2", "Estimate3", "Estimate4", "Estimate5", and "Estimate6". ...
0
by: Gian Paolo Clarici | last post by:
I'm migrating one Asp page to Aspx. There is a recordset and I need to Reponse.write the content of one of its fields . That field comes from a Numeric(5,2) SQL datatype Its content is : 90.00 ...
24
by: Donald Grove | last post by:
I want to populate an array with values from an ado recordset (multiple rows) I use the absolute position of the cursor in the recordset to define the row of my array to be populated. I have a...
1
by: sphinney | last post by:
All, I have a ADODB.Recordset in my Access 2002 project. I've been able to successfully add fields to the record set. According the the MS Access help files, I now must update the recordset to...
1
by: kccoolrocks1981 | last post by:
I need to create a temporary table in VBA Code but recordset.fields(i).Type gives me an integer value instead of the required field's Type. For an example Field_Name= "CriteriaName" and it's...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.