|
Random Quotes from a database
We are going to create a random quotes application
using Access for the database and a script to display a random
entry .
Our database in this example is called quotes.mdb
with a table called tblquote with three fields id , quote and
author . The id will be a unique number , quote is the actual
text of the quote and author is the person responsible for the
quote. Here is a picture of our table.

Now we need to add some quotes to our database
, so search the internet and find a good resource for quotes
like the one below
Google
quote section
Now for our script to access the database and
display a quote at random.
<%
'define our ADO constants
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdTable = &H0002
'declare variables
Dim objConn , objRS , RandomNo
'create a n instance of connection object
Set objConn = Server.CreateObject("ADODB.Connection")
'open our quotes database
objConn.Open "DBQ=" & Server.MapPath("quotes.mdb")&
";Driver={Microsoft Access Driver (*.mdb)}"
'create an instance of the recordset object
Set objRS = Server.CreateObject("ADODB.Recordset")
'open tblquotes table
objRS.Open "tblquote" , objConn , adOpenStatic ,adLockOptimistic
, adCmdTable
'create a random number
Randomize
RandomNo = Int(Rnd * objRS.RecordCount)
'move recordset to random number
objRS.Move RandomNo
'createa table
Response.Write "<table>"
Response.Write "<tr><td>Random quote</td></tr>"
Response.Write "<tr><td>"
'display the quote
Response.Write objRS.Fields("quote")
Response.Write "</td></tr>"
Response.Write "<tr><td>"
'display the author
Response.Write objRS.Fields("author")
Response.Write "</td></tr>"
Response.Write "</table>"
'close and destroy objects
objRS.Close
Set objRS = Nothing
Set objConn = Nothing
%>
|