|
Random image from a database
This example shows how to create a database using
Access , in which we store the details of our image . We will
then display one of these images at random.
First start access and create the database , the
database is called postcards in this example and we called our
table tblpostcards . Here is the structure of the table

Now in this example the images are stored in the
same directory as the database . The first thing you need is
the images , they are available at the link below
images for this example
Now we need to add this information into our database
, enter the information like in the image below.

Now for the good part the script , here it is
in full
<%
Const adOpenForwardOnly = 0
Const adLockReadOnly = 1
'declare our variables
Dim objConn , objRS , strConnect , RandomNo
'create an instance of connection object
Set objConn = Server.CreateObject("ADODB.Connection")
'open our database
objConn.Open "DBQ=" & Server.MapPath("postcard.mdb")&
";Driver={Microsoft Access Driver (*.mdb)}"
'create an instance of the recordset object
Set objRS = Server.CreateObject("ADODB.Recordset")
'open the tblpostcard table
objRS.Open "Select * FROM tblpostcard" , objConn ,
3, , adCmdTable
Randomize
'create a random number between 1 and the amount of records
in the
'table and store this in RandomNo
RandomNo = Int(Rnd * objRS.RecordCount)
'move to the RandomNo record
objRS.Move RandomNo
'display the image
Response.Write "<img src = " & objRS.Fields("url")
& ">"
Response.Write "<br>"
'display our description
Response.Write objRS.Fields("description")
'tidy up by closing and destroying objects
objRS.Close
Set objRS = Nothing
Set objConn = Nothing
%>
|