D3: Handling a Key violation when posting to a table

Generate an OnPostError event for your table and add code similar to


If E.Message = 'Key violation.' then
   MessageDlg('There is already a record with a date of ' +
wwtblHvalueDate.AsString + '.' + #13 +
              'Choose another date or cancel this operation and Edit' +
#13 +
              'the existing date.' , mtWarning, [mbOk], 0);
   Action := daAbort;


E.Message is a standard var in the Handler.  'Key violation' is the
actual text passed to the std Delphi exception window.  Action=daAbort
makes the exception silent (so you don't see your own MessageDlg PLUS
the Delphi exception).  You won't win any programming awards using this
method, but it fits the bill when you only have the urge
to generate a few of your own exceptions...



Matt Peebles wrote:
> I wanted to provide a more intelligent message to the user when there
> is a Key
> violation.  I need help on how to check if the exception passed into
> the table's
> PostError event is actually a Key violation and not some other error.
> I wanted to
> add a try...except handler with my own MessageDlg but I cannot figure
> out how to
> check the exception.

> My exception handling skills are pretty weak and any info would be
> greatly
> appreciated.

> Thanks.