Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about getting top 20 records but not the top 5

Status
Not open for further replies.

neillovell

Programmer
Aug 27, 2002
560
GB
Hello!
I know I can use
SELECT * Top 20 ORDER BY TheDateField
or something similar to get the top 20 records in a database, but how can I get records 5-15 ? I'm thinking if I had several forms or web pages each with select records only. Link when you search on ebay or google and you have those numbers at the bottom.

Thanks.
 
You set the PageSize property of your recordset. When you open the recordset you will always get the first page of data anyway. You can navigate to a page by setting the AbsolutePage property. e.g.

Public Sub PageTest()
Dim R As ADODB.Recordset
Set R = New ADODB.Recordset
R.PageSize = 5 '5 items per page
R.Open "SELECT NumValue FROM tblBigNums ORDER BY NumValue DESC", CodeProject.Connection, adOpenStatic, adLockReadOnly
Debug.Print "There are " & R.PageCount & " pages"
R.AbsolutePage = 2
While R.AbsolutePage = 2 'Print all page 2 items
Debug.Print R("NumValue").Value
R.MoveNext
Wend
R.Close
Set R = Nothing
End Sub

Look up the help on PageSize - there is a reasonable example. Good question by the way - I had wondered about this myself but never got around to looking it up.
 
Use one query with the top value set to twenty. Use a second query with the first as the recordsource, order it in descending date order, and select the top fifteen.

I'm sure there has to be a simpler method; if anyone has a sql solution, I'd be very interested in seeing it.
 
SELECT COLUMNNAME TOP 20 WHERE COLUMNNAME NOT IN SELECT COLUMNNAME TOP 5
 
Hi n2756m,

I like it and it will usually suit, but a small caveat. It won't work if, say, numbers 5 and 6 are duplicates on columnname.

Enjoy,
Tony
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top