Display your Recordset in a 3 column Table Layout

The code below retrieves a recordset from our "tblFriends" table in the database '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'. The code displays the results in a 3 column table layout.

If you wish to change the number of columns displayed then simply change the line If iColumnSpan=3 Then to the number of columns you want.

<%@ Language="VBScript" %>
<% Option Explicit %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>3 column layout</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<%
'declare your variables
Dim Connection, Recordset
Dim sSQL, sConnString, iColumnSpan

'declare SQL statement that will query the database
sSQL="SELECT FirstName, SurName FROM tblFriends"

'define the connection string, specify database
'driver and the location of database
sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("Friends.mdb")

'create an ADO connection and recordset
Set connection = Server.CreateObject("ADODB.Connection")
Set recordset = Server.CreateObject("ADODB.Recordset")

'Open the connection to the database
connection.Open sConnString

'Open the recordset object, execute the SQL statement
recordset.Open sSQL,connection

'first of all determine whether there are any records
If Recordset.EOF Then
Response.Write("No records returned.")
Else

Response.write "<table width=""100%"" border=""1"">"
'iColumnSpan is used as a column counter
iColumnSpan=0
response.write "<tr>"
'If there are records then loop through the fields
Do While Not recordset.EOF

If iColumnSpan=3 Then
response.write "</tr><tr>"
iColumnSpan=0
End If

response.write "<td>"
response.write recordset("FirstName") & " " & recordset("SurName")
response.write "</td>"

iColumnSpan=iColumnSpan + 1

'move on to the next record
Recordset.MoveNext
Loop

response.write "</tr>"
Response.write "</table>"

End If


'close the connection and recordset objects and free up resources
Recordset.Close
Connection.Close
Set Recordset = Nothing
Set Connection = Nothing
%>
</body>
</html>

Alternatively you could create a 3 column layout using the VBScript Mod Operator

Get the best asp hosting provider from web-hosting-top.com and save up to 30%

Advertisements



MembersPro

MembersPro PayPal - ASP Membership software

Plug and play ASP membership script that integrates with PayPal to let you charge recurring membership fees.

Global ASP.NET Hosting Leader - Click Here