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!

ASP - Access Database paging links <prev 1234......

Status
Not open for further replies.

techskool

Technical User
Jun 20, 2002
151
GB
Hi Guys.

Still relatively new to programming, Ive nearly finished my first asp page.

It just displays some records from an access database, and Im trying to write code to page the information.

ideally I would like <prev 1 2 3 * * * * next>

but at the moment the code just does PREV & NEXT, and even that is causing a problem. It displays the same 5 records as I cycle through the next link??

<!--Page encoded by David James Tosh, 2002/2003-->
<!--ASP page that process the information-->
<%@ Language = &quot;VBScript&quot;%>
<%
'Declare all local variables

'connections
dim conn
dim rs
dim strconn
dim sql

'page numbering
dim currentPage
dim NumRows


'set a local variable to my DSN-less connection String
strconn = &quot;DRIVER=Microsoft Access Driver (*.mdb);DBQ=&quot; & Server.MapPath(&quot;recordsnorthwest.mdb&quot;)

'Create the Connection object
set conn = server.createobject(&quot;adodb.connection&quot;)
conn.open strconn

sql = &quot;SELECT * FROM stock&quot;

'Create the recordset object
set rs = server.createobject(&quot;adodb.recordset&quot;)
'This statement opens the table so we can add a record notice the addnew
'The 2, 2 is how the table is opened
rs.open sql, conn, 3
rs.PageSize=5

%>

<html>
<head>
<title>View Database Contents</title>
</head>
<body>

<table >
<tr>
<th>Picture </th>
<th>Artist </th>
<th>Title </th>
<th>Label </th>
<th>Comment </th>
<th>Category </th>
<th>Year Made </th>
<th>Price </th>
<th>Mp3 </th>
<th>Cost </th>
<th>DateLogged </th>
<th>InStock </th>
<th>Owner </th>
</tr>

<%
NumRows=0
Do Until RS.EOF OR NumRows>=RS.PageSize
%>
<tr>
<td><img src= <td> <%=RS(&quot;Artist&quot;)%> </td>
<td> <%=RS(&quot;Title&quot;)%> </td>
<td> <%=RS(&quot;Label&quot;)%> </td>
<td> <%=RS(&quot;Comment&quot;)%> </td>
<td> <%=RS(&quot;Category&quot;)%> </td>
<td> <%=RS(&quot;YearMade&quot;)%> </td>
<td> <%=RS(&quot;Price&quot;)%> </td>
<td><a href=&quot; <td> <%=RS(&quot;Cost&quot;)%> </td>
<td> <%=RS(&quot;DateLogged&quot;)%> </td>
<td> <%=RS(&quot;InStock&quot;)%> </td>
<td> <%=RS(&quot;Owner&quot;)%> </td>
</tr>

<%
RS.MoveNext
NumRows=NumRows+1
Loop
%>

</table>
<br>
<a href=&quot;default.asp&quot;>Return</a> to main menu

<br>
<br>

<!--number pages next and previous-->
<div align=center>
<%
If currentPage>1 Then
%>
<a href=&quot;query.asp?page=<%=currentPage-1%>&quot;>Prev</a>
<%
End If
%>
<%
If currentPage<RS.PageCount Then
%>
<a href=&quot;query.asp?page=<%=currentPage+1%>&quot;>Next</a>
<%
End If
%>
</div>
</body>
</html>



If someone could help suggest how I can get this working, or even include numbers in between the prev and next links (like google uses) that would be awesome.

Ive been pulling my hair out.

MANY THANKS

Dave
 
Hi there,

The page you need is as follows, I have added a few bits in and changed some of the code to suit but what you have intended is there. It will give you a set of links at the bottom of the page which allows you to go to the first and last record in the set as well as move forwards and backwards.

I have put in a query variable so you can use it to pass a query through o the recordet. If you wish just change the SQL statement to something like

STRSql = “SELECT * from stock WHERE Artist=’” & query & “’”

That’s should pull up the query ok if an artist name was passed through.

If you need anything nesle just shout up
cheers.

'****************************************************
<%@language=vbscript%>
<%option explicit

Const adOpenForwardOnly = 0
Const adLockReadOnly = 1
Const adCmdTableDirect = &H0200
Const adUseClient = 3
dim strSQL, objRS, I, icount, records
Dim objConnection

objConnection = &quot;Provider=Microsoft.Jet.OLEDB.4.0; Data Source=&quot;
objConnection = objConnection & Server.MapPath(&quot;\recordsnorthwest.mdb &quot;)



records = request.querystring(&quot;records&quot;)


set objRS = server.CreateObject(&quot;ADODB.Recordset&quot;)
objRS.PageSize = 6
objRS.CacheSize = 300
objRS.CursorLocation = adUseClient


query = &quot;&quot;
strSQL = &quot;&quot;
icount = &quot;&quot;

'take all the form details and store them in the variables
query = Request.querystring(&quot;query&quot;)

strSQL = &quot;SELECT * FROM stock&quot;

objRS.Open strSQL, objConnection, adOpenForwardOnly, adLockReadOnly, adCmdTableDirect
%>

<html>
<head>
<title>Paging</title>
</head>
<body bgcolor=&quot;#ffffff&quot;>
<table width=&quot;600&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; align=&quot;center&quot;>
<tr>
<td width=&quot;58&quot; height=&quot;37&quot;></td>
<td bgcolor=&quot;#f7ca73&quot; height=&quot;37&quot; width=&quot;157&quot;><img src=&quot;IMAGES/Header.jpg&quot; width=&quot;151&quot; height=&quot;50&quot;></td>
<td bgcolor=&quot;#f7ca73&quot; height=&quot;37&quot; class=&quot;header&quot;>
<div align=&quot;right&quot;>HEADER</div>
</td>
<td width=&quot;39&quot; height=&quot;37&quot;></td>
</tr>
<tr>
<td width=&quot;58&quot; valign=&quot;top&quot;></td>
<td valign=&quot;top&quot; colspan=&quot;2&quot;><%if objRS.EOF and objRS.BOF then
response.write(&quot;Your search has returned no results, please re search the database using a different criteria.&quot;)
else
'***************************************
'start the loop in the recordset here
'***************************************
If Len(request.querystring(&quot;records&quot;)) = 0 then
objRS.AbsolutePage = 1
Else
If CInt(request.querystring(&quot;records&quot;)) <= objRS.PageCount then
objRS.AbsolutePage = request.querystring(&quot;records&quot;)
Else
objRS.AbsolutePage = 1
End if
End if

Dim abspage, pagecnt

abspage = objRS.AbsolutePage
pagecnt = objRS.PageCount



response.write(&quot;<TABLE WIDTH='100%' cellpadding=0 cellspacing=0 border=0>&quot;)
Dim Fields
For I = 1 to objRS.PageSize
If Not objRS.EOF Then
'write the table data onto the page with the recordset details.
%>
<tr>
<td><img src= <td> <%=RS(&quot;Artist&quot;)%> </td>
<td> <%=RS(&quot;Title&quot;)%> </td>
<td> <%=RS(&quot;Label&quot;)%> </td>
<td> <%=RS(&quot;Comment&quot;)%> </td>
<td> <%=RS(&quot;Category&quot;)%> </td>
<td> <%=RS(&quot;YearMade&quot;)%> </td>
<td> <%=RS(&quot;Price&quot;)%> </td>
<td><a href=&quot; <td> <%=RS(&quot;Cost&quot;)%> </td>
<td> <%=RS(&quot;DateLogged&quot;)%> </td>
<td> <%=RS(&quot;InStock&quot;)%> </td>
<td> <%=RS(&quot;Owner&quot;)%> </td>
</tr>

<%
objRS.movenext
end if
Next



'***************************************
'end the loop in the recordset here
'***************************************
response.write(&quot;</table>&quot;)
end if

objRS.close
set objRS = nothing

%>
</td>
<td width=&quot;39&quot;></td>
</tr>
<tr>
<td width=&quot;58&quot; height=&quot;2&quot;></td>
<td height=&quot;2&quot; class=&quot;footer&quot; bgcolor=&quot;#ffffff&quot; colspan=&quot;2&quot;>
<div align=&quot;right&quot;>
<%
'********************************************************
'put the navigation links on the bottom of the page here
'********************************************************

Response.Write &quot;<div align='right'>&quot; & vbcrlf
Response.Write &quot;<a href='&quot;
Response.Write &quot;search.asp&quot;
Response.Write &quot;?records=1&query=&quot;& request.querystring(&quot;query&quot;) & &quot;'>First Page</a>&quot;
Response.Write &quot; | &quot;

If abspage = 1 Then
Response.Write &quot;<span style='color:silver;'>Previous Page</span>&quot;
Else
Response.Write &quot;<a href='&quot; & Request.ServerVariables(&quot;SCRIPT_NAME&quot;)
Response.Write &quot;?records=&quot; & abspage - 1 & &quot;& query =&quot; & request.querystring(&quot;query &quot;)& &quot;'>Previous Page</a>&quot;
End If

Response.Write &quot; | &quot;
If abspage < pagecnt Then
Response.Write &quot;<a href='&quot; & Request.ServerVariables(&quot;SCRIPT_NAME&quot;)
Response.Write &quot;?query =&quot; & request.querystring(&quot;query&quot;) & &quot;&records=&quot; & abspage + 1 & &quot;'>Next Page</a>&quot;
Else
Response.Write &quot;<span style='color:silver;'>Next Page</span>&quot;
End If

Response.Write &quot; | &quot;
Response.Write &quot;<a href='&quot; & Request.ServerVariables(&quot;SCRIPT_NAME&quot;)
Response.Write &quot;?query=&quot; & request.querystring(&quot;query&quot;) & &quot;&records=&quot; & pagecnt & &quot;'>Last Page</a>&quot;
Response.Write &quot;</div>&quot; & vbcrlf




'********************************************************
'end the navigation links on the bottom of the page here
'********************************************************
%></div>
</td>
</tr>
<tr>
<td width=&quot;58&quot; height=&quot;2&quot;></td>
<td height=&quot;2&quot; class=&quot;footer&quot; bgcolor=&quot;#f7ca73&quot; colspan=&quot;2&quot;>
</td>
</tr>
<tr>
<td width=&quot;58&quot; height=&quot;2&quot;></td>
<td height=&quot;2&quot; bgcolor=&quot;#f7ca73&quot; colspan=&quot;2&quot;>

</td>
</tr>
</table>
</body>
</HTML>
'mi casa es su casa'
]-=tty0=-[
ICQ:82621399
 
Thanks so much tty0.

I havent implemented it yet, Im going to today, but I understand where you are coming from, I think.

Thanks again

Dave

 
Your welcome, to see it in action try this:


and do a select all search to get a decent amount of data.

Any probs just shout up
cheers 'mi casa es su casa'
]-=tty0=-[
ICQ:82621399
 
Im going to use that script on part of my site, yet theres another paging script ive tried to include for a different part of the site, that I cant get working.

Ive written an asp page that searches a caterogy of music genre on my access database.

When the results are returned , Artist Title, Comments etc, I would like to be able to page through the results.

I have included the code for one of my query pages.

It works, but must have an error in it, and I cannot for the life of me, figure it out.

It should display all records up to 5 per page, and add next and previous links at the bottom of the page, if more than 5 records have been found, and in the case I am testing I know it should return 10 records!

The strange thing is lets say I successfully query 10 records.

Only 5 are returned, however their is no next button?

I change the constant iPageSize=5 to iPageSize=6 or 7 or 8, and it is the same.

However if I change iPageSize=5 to iPageSize=4 it still displays the 5 records from the first page, only now it has a next button that takes me to a second page with one record on, a duplicate of the last record from the first page!

Also if I change iPageSize=5 to iPageSize=3 it still displays the 5 records from the first page, again it has a next button that takes me to a second page with one record on, this time a duplicate of the last two records from the first page!

I am absolutely confused.

I am looking at the loop, but I cant see any errors, as it all views fine.

If anyone can offer some advise, and have a look at this code, it would be smashing.

Its prety well anotated (i think)

CODE
-----------------------
<!--#include File=&quot;adovbs.inc&quot;-->
<%
dim conn
dim rs
dim strconn
dim sql
dim searchTerm

Const iPageSize=5 'How many records To show
Dim CPage 'Current Page No.
Dim TotPage 'Total No. of pages if iPageSize records are displayed per page.
Dim i 'Counter

'Paging
CPage=Cint(Request.Form(&quot;CurrentPage&quot;)) 'get CPage value from form's CurrentPage field
Select Case Request.Form(&quot;Submit&quot;)
Case &quot;Previous&quot; 'if prev button pressed
CPage = Cint(CPage) - 1 'decrease current page
Case &quot;Next&quot; 'if next button pressed
CPage = Cint(CPage) + 1 'increase page count
End Select

'set a local variable to my DSN-less connection String
strconn = &quot;DRIVER=Microsoft Access Driver (*.mdb);DBQ=&quot; & Server.MapPath(&quot;recordsnorthwest.mdb&quot;)
'Create the Connection object
set conn = server.createobject(&quot;adodb.connection&quot;)
conn.open strconn

searchTerm=&quot;Old Skool&quot;
'Create the recordset object
set rs = server.createobject(&quot;adodb.recordset&quot;)

sql=&quot;SELECT * FROM stock WHERE Category LIKE '%&quot;&searchTerm&&quot;%'&quot;
rs.open sql, conn, 3

'paging
Rs.PageSize=iPageSize
if CPage=0 Then CPage=1 'initially make current page = first page
if Not(Rs.EOF) Then Rs.AbsolutePage=CPage 'specifies that current record resides In CPage
TotPage=Rs.PageCount 'stores total no. of pages

%>

<head><title>Search For Old Skool</title>
<link href=&quot;css/main.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot;>
</head>
<body>
<div align=center><img src=&quot;images/recordsnorthwest_logo.jpg&quot; width=&quot;422&quot; height=&quot;34&quot;></div>
<h2>Old Skool Search</h2>

<table border=&quot;1&quot; >
<tr>
<th>Picture </th>
<th>Artist </th>
<th>Title </th>
<th>Label </th>
<th>Comment </th>
<th>Year Made </th>
<th>Price </th>
<th>Mp3 </th>
</tr>

<%
For i=1 To Rs.PageSize
DO Until RS.EOF
%>
<tr align=&quot;center&quot;>
<td><img src=&quot;<%=RS(&quot;Picture&quot;)%>&quot;></td>
<td> <%=RS(&quot;Artist&quot;)%> </td>
<td> <%=RS(&quot;Title&quot;)%> </td>
<td> <%=RS(&quot;Label&quot;)%> </td>
<td> <%=RS(&quot;Comment&quot;)%> </td>
<td> <%=RS(&quot;YearMade&quot;)%> </td>
<td> <%=RS(&quot;Price&quot;)%> </td>
<td><a href=&quot;<%=RS(&quot;Mp3&quot;)%>&quot;><img src=images/mp3_button.gif border=0></a></td>
</tr>

<%
Rs.MoveNext
loop
%>

<%
if Rs.EOF Then Exit For
Next
'close all connections and recordsets
Rs.Close
Conn.Close
Set Rs = Nothing
Set Conn = Nothing
%>

</table>
<br>
<!-- ***************************** FINISH *************************** -->
<!-- Link to go back to the start page. -->
<br><br><br>
<a href=&quot;default.asp&quot;>Cancel</a> back to menu

<br><br>
Page <%=CPage %> of <%=TotPage %><P>
<!--'store current page value In hidden Type and display next-prev buttons-->
<FORM Action=&quot;<%=Request.ServerVariables(&quot;SCRIPT_NAME&quot;) %>&quot; Method=POST>

<INPUT Type=Hidden name=&quot;CurrentPage&quot; Value=&quot;<%=CPage%>&quot; >
<% if CPage > 1 Then %>
<INPUT type=Submit Name=&quot;Submit&quot; Value=&quot;Previous&quot;>
<% End if%>
<% if CPage <> TotPage Then %>
<INPUT type=Submit Name=&quot;Submit&quot; Value=&quot;Next&quot;>
<% End if %>
</FORM>
</body>
</html>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top