Display Top 10 Records

Sometimes when you display records from a database on the web, you only want to display the most recent records.  For example, if you allow customers to view their order history on your website, you may want to display the 10 most recent orders first by default.  Here is how you do it.

First, create a database called MyData and create a table called tblOrders with these fields:
ID – autonumber
Orders – text field
DateEntered – date/time field

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

Next, 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

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


iRecordCount = iRecordCount + 1

Set objRS = Nothing
Set objConn = Nothing

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

< Back to ASP Scripts