The code below pages the records returned from our database 'myLinks.mdb'.
The table is called 'tblLinks' and has 3 fields, an 'ID' (autonumber
field), a textfield called 'SiteName' and a memo field called 'URL'.
This script can be easily modified to suit your requirements. (See
our recordset paging script in practice)
<%
Option Explicit
Dim Currpage, pageLen, lastNumber, PageRem, PageTen
Dim connection, recordset, sSQL, sConnString, next10, prev10,
P
Dim RSPrevPage, RSNextPage, start
If IsEmpty(Request.Querystring("PageNo")) then
CurrPage = 1
Else
CurrPage = Cint(Request.Querystring("PageNo"))
End If
Function getNext10(num)
pageLen = len(num)
If pageLen = 1 Then
next10 = 10
Else If pageLen>1 Then
pageRem = 10
pageTen = right(num, 1)
next10 = num + pageRem - pageTen
End If
End If
getNext10 = next10
End Function
Function getPrev10(num)
pageLen = len(num)
If pageLen = 1 then
prev10 = 1
Else If pageLen>1 then
lastNumber = right(num, 1)
prev10 = num - lastNumber - 10
End If
End If
If prev10 = 0 then
prev10 = 1
End If
getPrev10 = prev10
End Function
Set Connection = Server.CreateObject("ADODB.Connection")
Set Recordset = Server.CreateObject("ADODB.Recordset")
sConnString = "DRIVER={Microsoft Access Driver (*.mdb)};"
& _
"DBQ=" & Server.MapPath("/codesnippets/db/mylinks.mdb")
& ";"
sSQL="SELECT * FROM tblLinks"
Connection.Open sConnString
Recordset.CursorLocation = 3
Recordset.open sSQL, sConnString
Recordset.PageSize = 10
%>
<html>
<head><title>Recordset Paging Script</title>
</head>
<body>
<%
next10 = getNext10(CurrPage)
prev10 = getPrev10(CurrPage)
If Recordset.EOF Then
Response.write "No records to display"
Else
Recordset.AbsolutePage = CurrPage
Do Until Recordset.AbsolutePage <> CurrPage OR Recordset.Eof
response.write "ID: " & Recordset ("ID") & "<br>"
response.write "Sitename: " & Recordset ("Sitename") & "<br>"
response.write "URL: " & Recordset ("URL") & "<br><br>"
Recordset.MoveNext
Loop
End If
RSPrevPage = CurrPage -1
RSNextPage = CurrPage + 1
If Next10 > Recordset.PageCount Then
Next10 = Recordset.PageCount
End If
If prev10 = 1 AND next10 - 1 <
10 Then
start = 1
Else
start = Next10 - 10
If right(start, 1) > 0 Then
start = replace(start, right(start, 1), "0")
start = start + 10
End If
End If
If Recordset.PageCount > 1 Then
If currpage > 1 Then
response.write("<a href=""paging.asp?PageNo=" & Prev10 & """><<</a> ")
End If
If NOT RSPrevPage = 0 then
response.write("<a href=""paging.asp?PageNo=" & RSPrevPage & """><</a> ")
End If
For P = start to Next10
If NOT P = CurrPage then
response.write("<a href=""paging.asp?PageNo=" & P & """>" & P & "</a> ")
Else
response.write(" <b>" & P & " </b>")
End If
Next
If NOT RSNextPage > Recordset.PageCount Then
response.write("<a href=""paging.asp?PageNo=" & RSNextPage & """>></a> ")
End If
If NOT Next10 = Recordset.PageCount Then
response.write(" <a href=""paging.asp?PageNo=" & Next10 & """>>></a>")
End If
End If
Recordset.Close
Set Recordset = Nothing
Connection.Close
Set Recordset =Nothing
%>
</body>
</html>
If you are having problems when no records are found check out a forum post on this issue here.
If you have any code snippets to share with full credit given then send an email to Codesnippets - You'll receive full credit and a link back to your site.