Display Top 10 Records ASP Script - ASP Web Pro
  Active Server Pages Programming by ASP Web Pro

ASP Scripts

DISPLAY TOP 10 RECORDS

Sometimes when you display records from a database on the web, you only want to display the most recent records. The Our Latest Scripts list on our home page only displays the top 10 records ordered by date. Here is how you do it.

The first thing you need to do is create a database called MyDatabase. Then create a table called tblScripts with these fields:
ID - autonumber
Script - text field
DateEntered - date/time field *** Also add "=Date()" as the default value for the field. This will add the date automatically, everytime a new record is entered. ***

Then, start adding some data to your database. If you leave the database empty, the script will simply display a blank page.

Next, you create a page called displaytop10.asp and copy the below code into your page:

<!--#INCLUDE VIRTUAL="/includes/connection.asp" -->

<%
DIM mySQL, objRS
mySQL = "SELECT TOP 10 * FROM tblScripts ORDER BY DateEntered DESC"
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open mySQL, objConn
%>

<table border="1" width="100%"><tr><td align = "center">
Our Latest Scripts
</tr></td>

<%
DIM iRecordCount
iRecordCount = 0
DO WHILE NOT objRS.EOFand iRecordCount <> 10
%>

<tr><td><%=objRS("Script")%><tr><td>

<%
iRecordCount = iRecordCount + 1
objRS.MoveNext
Loop

objRS.Close
Set objRS = Nothing
objConn.Close
Set objConn = Nothing
%>
</table>

This will display the top 10 most recent records that were entered into your database.

< Back to ASP Scripts


 

 

Main Menu
Home
ASP Scripts
ASP Tutorials
JavaScripts
Awards
Contact Us

Email This Page

Other Resources
ASP Web Hosting
Search Engine Submission
Programming Help

 

 

 

Other ASP Web Sites

Site Map

 

Last Updated:
21 March 2010