Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 33,576 times

Related Categories

Paginate a recordset

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

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • paginate - asp

    Posted by sen_selva on 03 Nov 2004

    Dear James

    Kindly give the example

  • paginate - ASP

    Posted by sen_selva on 03 Nov 2004

    Error Type:
    ADODB.Recordset (0x800A0BB9)
    Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.
    /country/result.asp, line 80

  • Comments & Request

    Posted by Pargunan on 07 Oct 2003

    Hi James Crowley

    Thanks for your sample code, but if you provide with sample ASP page that will call this sample code. May be you can provide asp page that will display navigation buttons like firs...