Trap key violation

ado, delphi, delphi-7

Solution

This will work, if the only error you're wanting to handle is the one with the 'duplicate value' message:

try
  // Your code
except
  on E: EOleException do
  begin
    // The better way is to find out what E.ErrorCode is
    // for this specific exception, and handle it instead
    // of checking the string - you didn't provide the
    // ErrorCode, though.
    // If E.ErrorCode = <whatever> then
    //
    if Pos('duplicate value', E.Message) > 0 then
      // You've got a duplicate with the message above
      // Do whatever handles it
    else
      raise;
  end;
  // If you want to handle other exception types (eg., EDataBaseError),
  // you can do so here:
  //  on E: EDataBaseError do
  //    HandleDBError;
end;

Problem

hi i'm trying to do some exception handling and intercept a repeated field value (key violation) error. From my searching for a solution ive seen many suggestions to trap all errors using ``` try (enter code) except on E: EDatabaseError do showmessage (Error message); end; ``` but I'd like to respond specifically to a key violation, it uses an access table using ADO.

Original source

Related problems