View Full Version : next rec, previous rec

04-15-2006, 12:35 AM
Is there a way to create "next" and "previous" buttons on an item page?
It seems to be a hard work to let the server load all the records and then finding the next or the previous.
Is there a 'cheaper' way?


04-19-2006, 04:51 PM
Well...when loading the page you're selecting the information for that Item and I would assume that the item has an itemkey in the table.

So couldn't you just select the itemkey + 1 and itemkey - 1.

Of course...assuming you want to cycle straight down the db.

04-19-2006, 05:03 PM
it has an item key.
but it's not subsequent :)

it was very easy to program if it was :)

04-19-2006, 05:15 PM
The best way to do this is like the below. You just query for the one record so you don't have to worry about retrieving all records. The query string is then used to pick which record. This also allows people to bookmark an item, which is really recommended.

Example URL: http://site.com/itemdisplay.asp?item=1

item_num = Request.QueryString("item") - 1
query = "SELECT * FROM table LIMIT " & item_num & ", 1"
' Run query
' Query will pick whatever record is chosen in the query string
The previous button (display if item_num is greater than 0):

Response.Write "<a href=""itemdisplay.asp?item=" & item_num-1 & """>Next</a>"
The next button (display if item_num is less than the record count):

Response.Write "<a href=""itemdisplay.asp?item=" & item_num+1 & """>Next</a>"

04-19-2006, 07:30 PM
Is the limit clause works with Access?
Where can I read about it (I googled it and didn't find something useful).

04-19-2006, 09:29 PM
Sorry, didn't realize you were using Access. Should of figured since this is ASP. Access is a bit weird in this area (along with MS SQL Server). My previous solution is with most other databases like MySQL and PostGreSQL.

You can do something like:

item_num = Request.QueryString("item")
If (item_num > 1) Then
query = "SELECT TOP " & item_num & " * FROM table WHERE itemkey NOT IN (SELECT TOP " & item_num - 1 & " * FROM table)"
query = "SELECT TOP " & item_num & " * FROM table"
End If

You can just query the entire recordset and you can use the Move command on the recordset and specify the record number from the query string.