Although SQL Server an Access support the TOP
keyword to limit
the amount of records returned, you cannot specify a starting value; making it
unsuitable for use when paginating a recordset (ie displaying records over multiple
pages, as in a web directory). Fortunately, ADO provides an alternative:
'create a recordset object
Set rItems = Server.CreateObject("ADODB.Recordset")
'set the cursor location and type
rItems.CursorLocation = 3' adUseClient
rItems.CursorType = 3 'adOpenStatic
'number of rows to cache at a time. Should be set to the same as PageSize
rItems.CacheSize = 10
'number of items to display per 'page'
rItems.PageSize = 10
'execute the SQL query, and keep the recordset open
'you cannot use the Execute statement for this.
rItems.Open sSQL, cConn
'check if empty
If rItems.EOF Then
'no rows
Else
'set current page
rItems.AbsolutePage = Request.QueryString("page")
'get the total number of records
nItemCount = rItems.RecordCount
'get the number of pages
nPageCount = rItems.PageCount
'loop through an display the items in the recordset
Do While Not rItems.EOF and nItem < rItems.PageSize
'do something
rItems.MoveNext 'move to the next
record
nItem=nItem+1 'increment count
Loop
End If
'close the recordset
rItems.Close
'and destroy the object...
Set rItems = Nothing
Comments