Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking for duplicate records before inserting

Status
Not open for further replies.

stlWebWiz

Technical User
Mar 7, 2005
12
0
0
US
Hello,

I'm trying to determine if a record exist before I insert a record. Can anyone point me in the right direction here??

Heres how my code looks right now with nothing checking for duplicate records. An example or code snippet would really help and be appreciated.

<%
Dim name, email, country, sql_insert

name= request.form("name")
email = request.form("email")
country = request.form("country")

'Connection String is here

sql_insert="insert into users (name, email, country) values ('"& name &"','"& email &"', '"& country &"')"

con.execute sql_insert

con.close
Set con = Nothing
%>
 
Depending on the unique identifier that you are trying to run the check on depends on the solution, but here's an example using your existing code:
Code:
<%
Dim name, email, country, sql_insert

name = request.form("name")
email = request.form("email")
country = request.form("country")

'Connection String is here

'need to create a recordset to check against
dim check_sql, exists

if len(name)>0 then
  exists=true
  check_sql="select * from users where name='" & name & "'"
  set rs = con.execute(check_sql)
  if rs.eof then
    exists=false
  else
    exists=true
  end if
  set rs = nothing
end if

if exist=true then
  sql_insert="insert into users (name, email, country) values ('"& name &"','"& email &"', '"& country &"')"
  con.execute sql_insert
end if
con.close
Set con = Nothing
%>
 
Try something like this:

<%
Dim name, email, country, sql_insert

Dim rs = Server.CreateObject(ADODB.RecordSet)

name= request.form("name")
email = request.form("email")
country = request.form("country")

'Connection String is here

strSQL = "Select * from yourtable where name='"&name&"' AND email='"&email&"' AND country='"&country&"' "

rs=con.execute(strSQL)

if rs.EOF AND rs.BOF then

sql_insert="insert into users (name, email, country) values ('"& name &"','"& email &"', '"& country &"')"

con.execute sql_insert
Else

Response.Write "User already exists"

End if

con.close
Set con = Nothing
%>


I did not test the code...just giving you an idea...

 
Can you insert a record with an existing recordset open? I didn't know you could do that.
 
stlWebWiz,

What exactly are you trying to accomplish here? Besides the Unique ID per record the only thing I see you can possibly use that would be unique is the email. Because name and country can't be unique So, why not do a simple query on email and if it exists then they can't submit the data.

Code:
name= request.form("name")
email = request.form("email")
country = request.form("country")

'Connection String is here
sql = "SELECT email From users WHERE email='" & email & "';"

objRS.Open sql, con

If objRS.EOF Then ' No match, insert into database

sql="insert into users (name, email, country) values ('"& name &"','"& email &"', '"& country &"')"

objRS.Close

Else ' Match

Response.Write "Sorry, user exists!"

objRS.Close
con.close
Set objRS= Nothing
Set con = Nothing
End If
%>
 
Thanks to all of you for your input, They give me three ways of accomplishing what I set out to do. I'll play around with these and see what I which one I like the best, they all pretty good

STLWW
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top