Solved

How to limit ADO recordsets

Posted on 2002-06-26
15
162 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

688 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