Displaying the top 10 records
The code below will simply show the top 10 records from our "tblFriends"
table in the database called 'Friends.mdb'. The table has 3 columns,
firstly an 'ID' field that is an autonumber, secondly a field called
'FirstName' which is a textfield and lastly another textfield called
'SurName'.
Call our file 'display_records.asp'
<%@ Language="VBScript" %>
<% Option Explicit %>
<html>
<head>
<title>Displaying all records from a database table</title>
</head>
<body>
<%
Dim Connection, Recordset
Dim sSQL, sConnString
sSQL="SELECT TOP 10 ID, FirstName, SurName FROM tblFriends"
sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("Friends.mdb")
Set connection = Server.CreateObject("ADODB.Connection")
Set recordset = Server.CreateObject("ADODB.Recordset")
connection.Open sConnString
recordset.Open sSQL,connection
If Recordset.EOF Then
Response.Write("No records returned.")
Else
Do While Not recordset.EOF
Response.Write recordset("ID") & " " &
recordset("FirstName") & " " & _
recordset("SurName")
Response.Write "<br>"
Recordset.MoveNext
Loop
End If
Recordset.Close
Connection.Close
Set Recordset = Nothing
Set Connection = Nothing
%>
</body>
</html>
You can change the number of records you want to retrieve and display
by simply changing the number.
If there are no entries then the message 'No records returned.' will
be displayed.
Site developed by Michael Wall - Web Design Belfast N.Ireland.
Copyright © 2000-2008. All rights reserved.
|