This is the full code I'm using:
<%
dim cn, rs
set cn = server.createObject("ADODB.Connection")
set rs = server.CreateObject("ADODB.Recordset")
function makeDBConnection()
'set the file path to the database
dbPath="articles.mdb"
err.clear
on error resume next
' connect to the database
cn="driver={Microsoft Access Driver (*.mdb)};"
cn=cn & "dbq=" & server.mappath(dbPath)
if err then
Response.Write("<h3>Error Trapped</h3> while creating DB connection<p>" & err.description)
Response.End
end if
end function
function makeRS(SQL)
'Create a recordset
on error resume next
rs.Open sql, CN
if err then
Response.Write("<p>Error Trapped<p>" & err.description & "<p>while reading from DB<p> " & SQL)
Response.End
end if
makeRS=rs
end function
%>
<% makeDBConnection() %>
<%
dim id
dim visitorIP
dim rating
dim cookie
dim cookieRated
id = Request.Form("id")
rating = Request.Form("rating")
visitorIP = Request.ServerVariables("REMOTE_ADDR")
cookie = Request.Cookies("rate_" & id)
rs.activeconnection = cn
if cookie = "" then
cookieRated = false
else
cookieRated = true
end if
if rating = "" then
'Invalid rating
%>
<b>You must select a rating first!!</b>
<%
else
'Valid rating, make sure visitor hasn't already voted by checking the ratings table
rs.open "select count(*) from ratings where ip='" & visitorIP & "' and id=" & id
if rs.Fields(0).Value = 0 then
if cookieRated = false then
'Visitor hasn't rated yet, let's add it
cn.Execute "INSERT INTO ratings(rating, ip, id) VALUES(" & rating & ", '" & visitorIP & "', " & id & ")"
Response.Cookies("rate_" & id) = true
Response.Cookies("rate_" & id).expires = Date() + 30
%>
<b>Thanks for the rating.</b>
<%
else
'Visitor has already rated this article
%>
<b>Hey, you've already rated this article.</b>
<%
end if
else
'Visitor has already rated this article
%>
<b>Hey, you've already rated this article.</b>
<%
end if
end if
%>
The code in green is an include file, called "db.asp", but I've added that code too. The code in red is my "rate.asp" file, and the snippet in blue is the one I'm having a problem with.
The original code was written for a DSN connection, whereas I have customized it for my DSNless connection. That one line of code:
cn.Execute "INSERT INTO ratings(rating, ip, id) VALUES(" & rating & ", '" & visitorIP & "', " & id & ")"
is the biggest culprit. And it gives me the following error:
Error Type:
Microsoft VBScript runtime (0x800A01A8)
Object required: 'driver={Microsoft Ac'
It's a DSN command, and I have no idea what to put in there for a DSNless command. I've tried using:
sql="insert into ratings SET rating='" & rating & "',ip='" & visitorIP & "',id='" & id & "' where ratingid=" & request("ratingid")
and
sql="update ratings SET rating='" & rating & "',ip='" & visitorIP & "',id='" & id & "' where ratingid=" & request("ratingid")
but neither seems to work, since although the page is processed, the data is not added to the database.
Hope it's clear enough now, since I've posted the full code.
If you'd like to see the very original script, please do follow this url: http://www.devarticles.com/art/1/141
Edited by Nischint