display error message if there is a duplicate key

Results 1 to 2 of 2

Thread: display error message if there is a duplicate key

  1. #1
    Join Date
    Dec 1969

    Default display error message if there is a duplicate key

    I have this application and instead of calling another recordset and testing it since it will rarely happen. If i could manipulate the Duplicate key was ignored. error and put back a user friendly message.<BR><BR>According to my book there is no ole db error object so i am not sure how to go about it.<BR><BR>Matt

  2. #2
    Join Date
    Dec 1969

    Default RE: display error message if there is a duplicate

    &#062; According to my book there is no ole db error object <BR><BR>Huh??? There sure is an ADO Error object, though.<BR><BR>This code, used with a table that has a field "name" which specifies "indexed, no duplicates", works quite well.<BR><BR>The numbers are "magic numbers" to match the two expected error numbers from the Insert. <BR><BR><BR>&LT;HTML&#062;&LT;BODY&#062;<BR><BR>&L T;%<BR>&#039 get a name from query string:<BR>theName = Request.QueryString("name")<BR>If theName = "" Then theName = "Bill" &#039 if not given, default<BR><BR>&#039 create and open the connection<BR>Set Conn = Server.CreateObject("ADODB.Connection")<BR>Conn.Op en "DRIVER=Microsoft Access Driver (*.mdb);" _<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;& "DBQ=" & Server.MapPath("bb.mdb")<BR><BR>Set RS = Server.CreateObject("ADODB.RecordSet")<BR>RS.Open "people",conn,3,3<BR><BR>RS.AddNew <BR>RS("name") = theName<BR><BR>Conn.Errors.Clear<BR><BR>&#039 trap errors only as long as needed!<BR>On Error Resume Next<BR>&nbsp;&nbsp;&nbsp;&nbsp;RS.Update<BR>&nbsp ;&nbsp;&nbsp;&nbsp;Set errors = Conn.Errors<BR>On Error GoTo 0<BR><BR>For Each e In errors<BR>&nbsp;&nbsp;&nbsp;&nbsp;If e.number = -2147217873 Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;Response.Write "&LT;FONT Color=Red&#062;Sorry, &#039" & theName _<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;& "&#039 is a duplicate of a name already in the db.&LT;/FONT&#062;&LT;BR&#062;"<BR>&nbsp;&nbsp;&nbsp;&nbsp ;ElseIf e.number = -2147217887 Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;Response.Write "(Ignoring the &#039multiple step&#039 bogus error message)&LT;BR&#062;"<BR>&nbsp;&nbsp;&nbsp;&nbsp;E lse<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;Response.Write "&LT;FONT Color=Red&#062;UNEXPECTED ERROR, #" & _<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;e.number & ": " & Left(e.description,80) & "&LT;/FONT&#062;&LT;BR&#062;" <BR>&nbsp;&nbsp;&nbsp;&nbsp;End If<BR>Next<BR><BR>On Error Resume Next <BR>&nbsp;&nbsp;&nbsp;&nbsp;RS.Close &#039 ignore errors from CLOSE call...<BR>On Error GoTo 0<BR><BR>%&#062;<BR><BR>&LT;HR&#062;<BR>&LT;FORM&# 062;<BR>&LT;INPUT Name="name" Size=80&#062;<BR>&LT;BR&#062;<BR>&LT;INPUT Type=Submit Value="Add that name..."&#062;<BR>&LT;/FORM&#062;<BR><BR><BR>&LT;/BODY&#062;&LT;/HTML&#062;<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts