Show selection
Up

Now we will make a page that displays selected fields only, instead of displaying all of them as viewtable.asp did. Also, we will display our records not in a table but in paragraphs separated by horizontal lines. We will assume that your in your database table only four fields are in use and that we wish to display these four fields only. They have meanings as follows:

bullettext1 = family name
bullettext2 = first name
bullettext3 = email
bullettext4 = phone number.

You might be wondering why we put the phone number in a text field rather than a number field. Unless we need to do calculations with numbers we often store the numbers in text fields. If we did not do this then we would not be able to store 'numbers' like this phone number, for example: (33)493665478.

Make a new page and save it immediately with the name viewselected.asp. In html view, replace any existing code with this:

<html><head><title>View Selected Fields</title></head><body>
<h1>View Selected Fields as Paragraphs</h1>
<a
href="addrecordform.htm">Add a Record</a>&nbsp;&nbsp;
<a
href="viewtable.asp">View Table</a><hr>
<!--#include file="dbconnect.asp"-->
<%

' text1= family name, text2 = first name, text3 = email, text4 = phone

Dim objRS 'Holds the recordset for the records in the database
Dim strSQL 'Holds the SQL query for the database

Set objRS = Server.CreateObject("ADODB.Recordset")

'Initialise the strSQL variable with an SQL statement to query the database
strSQL = "SELECT text1, text2, text3, text4 FROM main"

objRS.Open strSQL, objConn 'Open the recordset with the SQL query

'Loop through the recordset
Do While not objRS.EOF
     'Write the HTML to display the current record in the recordset
     Response.Write objRS("text1") & ", " & objRS("text2") & "<br>"   ' family name, first name
     Response.Write objRS("text3") & " " & objRS("text4") & "<hr>"   
' email, phone
     objRS.MoveNext    'Move to the next record in the recordset
Loop

'Reset server objects
objRS.Close
Set objRS = Nothing
objConn.close
Set objConn = Nothing
%>

</body></html>

Analysis:

bulletThe gray line is an instruction to the server to incorporate the contents of the 'include file' called dbconnect.asp into the page viewselected.asp before it is processed by the server.
bulletThe SQL (pronounced 'sequel') query copies only the fields that we actually need into the recordset (we could have used the * wildcard to copy all the fields into the recordset but this would slow down the loading of our page).
bulletA loop is used to write into the browser window the value of the text1 (family name) field of the current record, then the value of the text2 (first name) field etc. At the end of the loop the statement objRS.MoveNext  causes the next record to become the current record. The loop continues until the record is reached that contains the End of File (EOF) marker, and then we jump out of the loop and close the recordset and connection objects.
bulletThe html tag <br> (line break) causes the text output to jump to a new line and the html tag <hr> (horizontal rule) draws a horizontal line then sends text output to the next line.

Now you should go to the add record page to see how you can add records to your database.

 

Up Next