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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Duplicate Records

Status
Not open for further replies.

kaycee79

Technical User
Jan 10, 2004
82
GB

On my webpage for newsletter subscriber, i want to create a way
of informing the user they have subscribed to the newsletter, currently
i am presented with this message;

Error Type:
Microsoft JET Database Engine (0x80040E21)
The changes you requested to the table were not successful
because they would create duplicate values in the index,
primary key, or relationship. Change the data in the field
or fields that contain duplicate data, remove the index, or
redefine the index to permit duplicate entries and try again.
/new/addsubscribertodb.asp, line 27


this is the code that i am using


Code:
<!-- #include file=&quot;ProjectConnection.asp&quot; -->

<!-- METADATA TYPE=&quot;typelib&quot;
              FILE=&quot;c:\Program Files\Common Files\System\ado\msado15.dll&quot; -->

<%
	Dim strConnect
%>
<html>
<head>
<title>Add Event</title>
</head>
<body>
<%

	Dim objRS
	Set objRS = Server.CreateObject(&quot;ADODB.Recordset&quot;)
	objRS.Open &quot;Subscriber&quot;, strConnect, adOpenKeySet, AdLockOptimistic
	objRS.AddNew
	
	objRS.Fields(&quot;Subscriber&quot;) = Request.Form(&quot;txtSubscriber&quot;)
	objRS.Fields(&quot;Username&quot;) = Session(&quot;Username&quot;)
		

	objRS.Update
	objRS.Close
	Set objRS = Nothing

	Response.Redirect &quot;NewsletterConfirmation.asp&quot;
%>
</body>
</html>

Thanks in advance
 
run a sql check to see if the user is in the db, then present a message for the above, if not present insert data and show confirmation

Bastien

Any one have a techie job in Toronto, I need to work...being laid off sucks!
 
thanks for that, can you help me with the SQL query please?
 
I'm shure that somehow &quot;username&quot; fields it's treated as primary key(uniques values, therefor you cant add same username).



________
George, M
 
username is the primary key in that field, but if i try to add the same user, it comes up with the error type message, i want a custom message sayin 'you have already subscribed'
 
Then you should use something like this
Code:
<%
    Dim objRS
    Set objRS = Server.CreateObject(&quot;ADODB.Recordset&quot;)

    objRS.Open &quot;Subscriber&quot;, strConnect, adOpenKeySet, AdLockOptimistic
    objRS.Filter=&quot;Username='&quot;& Session(&quot;Username&quot;)&&quot;'&quot;
    objRs.Requery
    if objRs.RecordCount<=0 then
     objRS.AddNew  
     objRS.Fields(&quot;Subscriber&quot;) = Request.Form(&quot;txtSubscriber&quot;)
     objRS.Fields(&quot;Username&quot;) = Session(&quot;Username&quot;)      
     objRS.Update
     objRS.Close
     Set objRS = Nothing
     Response.Redirect &quot;NewsletterConfirmation.asp&quot;
    else
     'user already in.
    end if
%>


________
George, M
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top