philosullivan
asked on
Check if record exists before insert function or procedure.
Hi,
I'm looking for a function that retuns True(1) or False(0) if a record allready exists in a database .I'd like to use it to catch duplicates. before a new record is posted.
Also how it would be used when posting a new record.
PS if there is a better or easier way i'm all ears!
Thanks
I'm looking for a function that retuns True(1) or False(0) if a record allready exists in a database .I'd like to use it to catch duplicates. before a new record is posted.
Also how it would be used when posting a new record.
PS if there is a better or easier way i'm all ears!
Thanks
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
'see if item exists BEFORE trying to insert into database
Function itemExists()
Dim qSQL
Dim conn
Dim rs
Dim totalrecs
Dim result
'inittize totalrecs
totalrecs=0
'initialize result
result=""
Set conn = Server.CreateObject("ADODB.Connection")
Set rs = Server.CreateObject("ADODB.RecordSet")
'open database connection
conn.Open "test"
'query here
qSql ="SELECT Count(*) as reccount FROM tbltest" + " WHERE fldtest=" + "'" + "Apple" + "'"
'execute query here
Set rs = Conn.Execute(qSQL)
'assign record count to totalrecs
totalrecs = rs("reccount")
'debug
'response.Write(cint(totalrecs))
if cint(totalrecs) > 0 then
'debug
'response.write("True")
result=True
end if
if cint(totalrecs) = 0 then
'debug
'response.write("False")
result=False
end if
' Close the Recordset object and destroy it
rs.Close
Set Rs = Nothing
conn.Close
Set Conn = Nothing
itemexists=result
'debug
response.Write("<br>")
response.Write(itemexists)
End Function
%>
<%
'insert record here
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>
<body>
<p><a href="<%=Request.ServerVariables("SCRIPT_NAME")%>?teststuff=1">Click Me</a>
<%
teststuff = Request.QueryString("teststuff")
If teststuff = 1 Then
Call itemexists
End If
%>
</p>
<p> </p>
<p> </p>
<p> </p>
</body>
</html>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.