Solved

How to limit ADO recordsets

Posted on 2002-06-26
15
159 Views
Last Modified: 2012-05-05
I need to be able to limit the number of records returned from a query against an Oracle database to 50 at a time without retieving all of the records to the client at once. After the user has viewed the first 50 records they should then be able to click a button that retrieves the next 50 records and so on. How would I do this?
0
Comment
Question by:dk02151
  • 7
  • 3
  • 3
  • +2
15 Comments
 
LVL 18

Expert Comment

by:deighton
ID: 7111290
how are you getting the reords?  Are these data bound controls?  or are you processing recordsets via code.
0
 

Author Comment

by:dk02151
ID: 7111346
We are getting the records through code only.
0
 
LVL 4

Expert Comment

by:gencross
ID: 7111390
Using ADO you can set the Maxrecords property to 50 before opening the recordset.

rstADO.MaxRecords = 50

I'll get back to you on getting the next 50 records.
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 4

Expert Comment

by:gencross
ID: 7111399
Ok.  What makes the most sense is retrieving all of the records each time.  And each time only show 50.  The first time show records 1-50.  The next time show records 51-100.  All you need to do is loop through the recordset until you get to the record you want to begin, and then show the next 50.

This way if you ever want to display 20 or 60 at a time all you will need to change is one variable.

It may sound like this could be slow, but after your initial search the records should be cached on the server and the next searches should be fast.

I believe this is the way quite a few internet search sites work.

If you need an example let me know.
0
 
LVL 3

Accepted Solution

by:
PNJ earned 200 total points
ID: 7112069
What about: "SELECT stuff FROM table WHERE selection criteria and ROWNUM <= 50 ORDER BY primary key

then when you hit the last record you can add " AND primary key >= <last value> AND ROWNUM <= 50".

ROWNUM is a inbuilt variable that Oracle defines that limits the number of records returned. It's up to you how to select the correct one(s) in the first place.
0
 
LVL 4

Expert Comment

by:gencross
ID: 7112089
PNJ, I thought something like that might work, but It seems it could cause problems if you want to change the order by at sometime.  I don't think it will work if you want to order the records by something else at sometime, perhaps for easier viewing.

Please let me know if I am wrong:)
0
 
LVL 2

Expert Comment

by:flytiger
ID: 7112469
'dim global variable current_index and max_length
max_length = 50

'in first recordset
current_index = 1

'when you click next
current_index = current_index + max_length

'sql statement here
sqlstmt="SELECT stuff FROM table WHERE selection criteria and ROWNUM >= " & current_index & " ROWNUM < " & (current_index + max_length)

0
 

Author Comment

by:dk02151
ID: 7113341
The selection criteria looks something like this:
"select * from TableName where PolicyNumber = 'XXXXXXX'"
This can retrieve several thousand cecords that are not consecutive with respect to the primary key which is an Oracle sequence number. Is there anything within the Oracle database itself that will do this? I am currently getting all of the records and displaying 50 at a time but getting all of the records can take too long when there is several thousand returned records.
0
 

Author Comment

by:dk02151
ID: 7113354
I see what you are saying now. I was not aware that ROWNUM was a built in Oracle variable. This looks like it might work. I will try it.
0
 

Author Comment

by:dk02151
ID: 7113356
I see what you are saying now. I was not aware that ROWNUM was a built in Oracle variable. This looks like it might work. I will try it.
0
 

Author Comment

by:dk02151
ID: 7114402
ROWNUM will not work. If I try doing ="SELECT stuff FROM table WHERE selection criteria and ROWNUM >= " & current_index & " ROWNUM < " & (current_index + max_length) I get no rows selected. ROWNUM only worked if the starting point is ROWNUM=1. In that case I am better of returning everything at once. Any other ideas?
0
 

Author Comment

by:dk02151
ID: 7114700
ROWNUM will not work. If I try doing ="SELECT stuff FROM table WHERE selection criteria and ROWNUM >= " & current_index & " ROWNUM < " & (current_index + max_length) I get no rows selected. ROWNUM only worked if the starting point is ROWNUM=1. In that case I am better of returning everything at once. Any other ideas?
0
 

Author Comment

by:dk02151
ID: 7114705
I got ROWNUM to work using the suggestion " AND primary key >= <last value> AND ROWNUM <= 50"
0
 
LVL 2

Expert Comment

by:flytiger
ID: 7114823
'Change your sql statement as following

sqlstmt="SELECT stuff FROM (SELECT stuff, rownum rn FROM table WHERE selection criteria) where rn >= " & current_index & " AND rn < " & (current_index + max_length)

'note: if you use ORDER BY. The result won't be correct anymore since ROWNUM was generated before ORDER BY
0
 
LVL 2

Expert Comment

by:flytiger
ID: 7114959
'if you want to use ORDER BY

sqlstmt="SELECT stuff FROM (SELECT stuff, rownum rn FROM (SELECT stuff FROM table WHERE selection criteria ORDER BY something)) WHERE rn >= " & current_index & " AND rn < " & (current_index + max_length)
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

770 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