All source code in ASP/ VbScript Ask a ASP/ VbScript Pro Discussion Forum Categories All jobs in ASP/ VbScript
Quick Search for:  in language:    
needed,able,page,through,database,records,una
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
ASP/ VbScript Stats

 Code: 281,486. lines
 Jobs: 101. postings

 How to support the site

 
Sponsored by:

 
You are in:
 
Login

NEW! LEARNING CENTER
Special educational offers, white papers, webcasts, podcasts

  NEW! Download Rational Performance Tester V8 (download now)
  NEW! Download Rational Functional Tester V8 (download now)
  NEW! Download Rational Service Tester for SOA Quality V8 (download now)
  NEW! Teleconference: Quality In Action - Using Rational Quality Manager with Functional, Performance and Web Service Testing Products (download now)
  NEW! Introducing IBM Rational AppScan Developer Edition – easing security testing by non-security professionals (download now)

 

 


Latest postings for ASP/ VbScript.
Click here to see a screenshot of this code!Mess Officer Management System (Sistem Pengurusan Mess Pegawai)
By Mohd Fauzi on 11/23

(Screen Shot)

Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!



 
 
   

Database Paging

Print
Email
 
VB icon
Submitted on: 7/6/2000 4:57:11 PM
By: Troy Demet 
Level: Beginner
User Rating: By 4 Users
Compatibility:ASP (Active Server Pages)

Users have accessed this code 34383 times.
 
 
     I needed to be able to page through a database of about 1000 records and I was unable to use .AbsolutePage so I had to try something else.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.

    '**************************************
    ' Name: Database Paging
    ' Description:I needed to be able to pag
    '     e through a database of about 1000 recor
    '     ds and I was unable to use .AbsolutePage
    '     so I had to try something else.
    ' By: Troy Demet
    '
    ' Assumes:You will need to make a connec
    '     tion to your own database and re-name to
    '     records.
    '
    'This code is copyrighted and has    ' limited warranties.Please see http://w
    '     ww.Planet-Source-Code.com/vb/scripts/Sho
    '     wCode.asp?txtCodeId=6252&lngWId=4    'for details.    '**************************************
    
    <%@ Language=VBScript %>
    <!--#include File="_fpclass/adovbs.inc"-->
    <%
    	gend = CStr(Request.QueryString("gender"))
    			
    ' **************** Added July 5, 2000 **
    '     **********
    Dim iPageSize 'How many records To show
    Dim iRecCurrent ' The page we want To show
    Dim sSQL 'SQL command To execute
    Dim RecSet	'The ADODB recordset object
    Dim I 'Standard looping var
    Dim iRecEnd	' Last Record
    Dim iRecMax	' Max of record Loop
    Dim J		' Loop variabel
    Dim iRecNext	' Var of Next record To start at
    Dim iRecPrev	' Var of Previous record
    Dim sGender	' Var For displaying whether Women's or Men's race
    Dim iNumPage	' Number of pages
    	
    ' Get parameters
    iPageSize = 20 		
    ' Retrieve page to show or default to 0
    if Request.QueryString("page") = "" Then
    iRecCurrent = 0	' First Record
    Else
    iRecCurrent = CInt(Request.QueryString("page"))
    End if
    	
    ' Assign value to race
    if gend = "Male" Then
    	sGender = "Men's"
    else
    	sGender = "Women's"
    End if
    	
    ' ****** End Added July 5, 2000 ********
    '     
    	
    '	SQL statement
    sSQL = "SELECT * FROM 5KResults WHERE Gender='"
    sSQL = sSQL & gend & "' ORDER BY Time"
    	
    Set RecSet = Server.CreateObject("ADODB.Recordset") 
    	
    RecSet.Open sSQL,"DSN=chiledadsn",adOpenForwardOnly,adLockReadOnly
    	
    '****** Added July 5, 2000 *************
    '     ****
    	
    ' Get the count of the records 
    Do While Not RecSet.EOF
    	J = J + 1
    	RecSet.MoveNext
    Loop
    iRecEnd = J -1
    	
    ' Get the number of pages
    iNumPage = CInt(iRecEnd/iPageSize)
    ' If the request page falls outside the 
    '     acceptable range,
    ' give them the closest match (0 or max)
    '     
    if iRecCurrent > iRecEnd Then iRecCurrent = iRecEnd 
    if iRecCurrent < 0 Then iRecCurrent = 0
    if iRecCurrent < iRecEnd Then
    iRecNext = iRecCurrent + iPageSize
    Else
    	iRecNext = iRecEnd
    End if
    	
    if iRecCurrent > 0 Then
    	iRecPrev = iRecCurrent - iPageSize
    Else
    	iRecPrev = 0
    End if
    	
    ' Do this so when calling the las page w
    '     e only loop through 
    ' the number of records we have if less 
    '     than the iPageSize
    if (iRecNext - iRecEnd ) > 0 Then
    	iRecMax = iRecEnd - iRecCurrent
    Else
    	iRecMax = iPageSize
    End if
    	
    '********End Added July 5, 2000 ********
    '     
    	
    ' Start at the beginning of the database
    '     
    RecSet.MoveFirst
    'Move to the record we want to start at
    RecSet.Move(iRecCurrent)
    	
    ' use this when creating links 
    ' doesn't matter what this page is named
    '     
    strScriptName = Request.ServerVariables("SCRIPT_NAME")
    	
    %>
    <%
    Sub NavBar()
    	Dim iPage
    	Dim iVue
    	Dim	sNumbers
    	Dim sPrev
    	Dim sNext
    	Dim sFirst
    	Dim sLast
    	Dim sNavBar
    	Dim iLastPage
    	iLastPage = iRecEnd - iPageSize
    			
    	For i = 0 To (iNumPage - 1)
    		iPage = i * iPageSize
    		iVue = i + 1
    				
    		sNumbers = sNumbers & NavLink(strScriptName,iPage,gend,iVue)
    									
    	Next
    				
    	if iRecCurrent <> 0 Then
    		sFirst = NavLink(strScriptName,0,gend,"First")
    		sPrev = NavLink(strScriptName,iRecPrev,gend,"Previous")
    	End if
    				
    	if (iRecCurrent + iRecMax) < iRecEnd Then
    		sNext = NavLink(strScriptName,iRecNext,gend,"Next")
    		sLast = NavLink(strScriptName,iLastPage,gend,"Last")
    	End if
    				
    	sNavBar = sNumbers & "<BR>" & sFirst & sPrev & sNext & sLast
    	Response.Write(sNavBar)
    End Sub
    			
    %>
    			
    <%
    ' Creates the link used by the navigatio
    '     n sub
    function NavLink(scriptName,pageNum,gendr, sWord)
    	Dim strLink
    			
    	strLink = strLink & "<A HREF='"
    	strLink = strLink & scriptName
    	strLink = strLink & "?page="
    	strLink = strLink & pageNum
    	strLink = strLink & "&gender="
    	strLink = strLink & gendr
    	strLink = strLink & "'>"
    	strLink = strLink & sWord
    	strLink = strLink & "</A>  "
    					
    	NavLink = strLink
    End function
    						
    %>		
    <HTML>
    <HEAD>
    	<TITLE>5K Race Results </TITLE>
    	
    	<META name="description" content="An example of paging through a database.">
    	<META name="keywords" content="Active Server Pages, ASP, database, paging">
    	<META http-equiv="Content-Type" content="text/html; charset=windows-1252">
    	<BASE target="_top">
    	<META name="language" content="en-us">
    	<META name="robots" content="INDEX">
    	<META name="revisit-after" content="14 days">
    	<META http-equiv="pragma" content="no-cache">
    </HEAD>
    <BODY>
    <!-- Database Table -->
    	
    <H3><% =sGender %> 5K Race</H3>
    <P><STRONG>Records</STRONG>: <% =iRecCurrent %> - <% = iRecCurrent + iRecMax %> of <% =iRecEnd %></P>
    <P><% NavBar %></P>
    <%
    ' Use these for debugging
    'Response.Write ("iRecCurrent: " & iRecC
    '     urrent & "<BR>")
    'Response.Write("iRecEnd: " & iRecEnd & 
    '     "<BR>")
    'Response.Write("iRecMax: " & iRecMax & 
    '     "<BR>")
    'Response.Write("iRecNext: " & iRecNext 
    '     & "<BR>")
    'Response.Write("iRecPrev: " & iRecPrev 
    '     & "<BR>")
    'Response.Write(CInt(iRecEnd/iPageSize) 
    '     & "<BR>")
    
    %>
    <TABLE border="0" cellPadding="1" cellSpacing="0" width="425px">
    <TR bgColor="blue">
    	<TD style="WIDTH: 130px" width="150" bgcolor="#388C40"><STRONG>Name</STRONG></TD>
    	<TD style="WIDTH: 35px" width="35" bgcolor="#388C40"><STRONG>Age</STRONG></TD>
    	<TD style="WIDTH: 90px" width="150" bgcolor="#388C40"><STRONG>City</STRONG></TD>
    	<TD style="WIDTH: 35px" width="45" bgcolor="#388C40"><STRONG>State</STRONG></TD>
    	<TD style="WIDTH: 50px" width="75" bgcolor="#388C40"><STRONG>Time</STRONG></TD>
    	<TD style="WIDTH: 50px" width="75" bgcolor="#388C40"><STRONG>Pace</STRONG></TD></TR>
    <%
    For i = 0 To iRecMax
    	if i mod 2 Then 
    	Response.write ("<TR bgColor=""#008080""><TD>")
    	Else
    		Response.Write("<TR><TD>")
    	End if
    	
    	Response.Write(RecSet("FirstName") & " ")
    	Response.Write(RecSet("LastName")& "</TD>")
    	Response.Write("<TD>" & RecSet("age") & "</TD>")
    	Response.Write("<TD>" & RecSet("City") & "</TD>")
    	Response.Write("<TD>" & RecSet("State") & "</TD>")
    	Response.Write("<TD>" & RecSet("Time" )& "</TD>")
    	Response.Write("<TD>" & RecSet("Pace") & "</TD>")
    	Response.Write("</TR>")
    			
    		
    ' Move to the next record
    	RecSet.MoveNext
    	
    Next
    ' Clean up after yourself
    	RecSet.Close
    	Set RecSet = Nothing
    			
    %>
    	</TABLE>
    <P><% Call NavBar %></P>
    		
    <!-- End Database Table -->
    	</BODY>
    </HTML>


Other 1 submission(s) by this author

 

 
 Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:
 
Your Vote!

What do you think of this code(in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
7/7/2000 1:17:56 AMjouniko

isn't there better way to count those records? ;) rs.RecordCount, ... And if you have a huge table with, let's say 100000 rows, why return them all if you want to see only 100-130.. use rs.MaxRecords = 130...
(If this comment was disrespectful, please report it.)

 
7/7/2000 11:52:13 AMJeff Taylor

jouniko, the RecordCount function doesn't work when the ADO recordset is opened in forward only mode (it returns a -1). Unfortunately the only method I have found effective (code and timewise) for a forward only record count is the same approach Troy uses here... the beautiful GWBASIC-esque loop-increment. Gotta love progress, eh?
(If this comment was disrespectful, please report it.)

 
7/7/2000 3:17:15 PMmyg

Unless I am totally mistaken, if you open forwardonly recordset and navigate forward using movenext(as when you are counting number of records), you can not go back to the first one ( or any previous one for that matter). Is this tested at all? (or I am missing the boat alltogether). Anyway, I wanted to put my two cents worth.
(If this comment was disrespectful, please report it.)

 
7/7/2000 3:40:30 PMJeffrey S. Taylor

you can MoveFirst in a forward-only cursor, but not MovePrevious.
(If this comment was disrespectful, please report it.)

 
7/10/2000 5:29:52 PMScott Whitlow

Is there a way to move from record to record in ASP without a complete page refresh? I need one part of my page to be static and the other part to be able to page through database records without leaving the page. Any suggestions? Thanks!
(If this comment was disrespectful, please report it.)

 
7/11/2000 1:09:44 PMTroy Demet

If you wish to see it working myq please got to www.chileda.org/resources/resources.htm .
This is where I needed to use it.
(If this comment was disrespectful, please report it.)

 
11/15/2000 5:15:24 PMJen

Just wanted to say thanks Troy! I am a beginner ASP programmer and needed good basic code to page through database records. Your's was the easiest for me to understand and only had to add a few more lines in order to transfer the original protocols from page to page.
Really appreciate it! jmurphy@cbu.edu
(If this comment was disrespectful, please report it.)

 
6/29/2001 12:45:32 PMChris

I am having problems with that too. You said you couldn't get RS.AbsolutePage working. Neither can I. I was told it only works in static or something else but I am in static and it keeps giving me a -1. If anyone knows how to solve this I REALLY need your help. ProgramMax@aol.com if you can help. Thanks!
(If this comment was disrespectful, please report it.)

 
10/23/2002 1:01:07 PM

Why come there isn't any sample to display your code at work.

(If this comment was disrespectful, please report it.)

 
10/23/2002 1:03:07 PM

Why come you don't have any example to show your code at work
(If this comment was disrespectful, please report it.)

 
2/12/2004 5:29:31 AM

I was trying to figure out the equation for finding out the current page, but I have not yet been successfull. Would anyone be willing to help? Thanks for the great work Troy.
(If this comment was disrespectful, please report it.)

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author from the email account you registered on the site, so you can correspond directly.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
To post feedback, first please login.


 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | ASP/ VbScript Home | Site Home | Other Sites | Open Letter from Moderators | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997-2008 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.   Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.