Rotating AD banners (database version)

What we are trying to create is a system where you store a banners image and url and then display a random banner on a web page . In this example we create a database called banners with atable called tblbanner like this.

Now insert your banners and the url that a visitor will go if and when they click on that banner, you will also have to create some banners , here are our banners added to the table.

Now all we need to do is create a script to display a banner at random.

<%
'our constants
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdTable = &H0002
'declare our variables
Dim objConn , objRS , RandomNo
'create an instance of the connection object
Set objConn = Server.CreateObject("ADODB.Connection")
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & Server.MapPath("banners.mdb") & ";"
objConn.Open strConn
'create an instance of the recordset object
Set objRS = Server.CreateObject("ADODB.Recordset")
'open banner table
objRS.Open "tblbanner" , objConn , adOpenStatic , adLockOptimistic , adCmdTable
'create a random number from 1 to the total amount of records
Randomize
RandomNo = Int(Rnd * objRS.RecordCount)
'move to random record
objRS.Move RandomNo
'display the url and then the image
Response.Write "<a href = '" & objRS.Fields("url") & "'>"
Response.Write "<img src = '" & objRS.Fields("image") & "'>"
'close recordset and destroy objects
objRS.Close
Set objRS = Nothing
Set objConn = Nothing
%>

 

Books

cover

XML Programming with VB and ASP

cover

Visual Basic Developer's Guide to Asp...

cover

Beginning Components for ASP