Board index » delphi » Check Duplicated Key (Key Violation)

Check Duplicated Key (Key Violation)

Hello all!
Would you please tell me what is the best method to implementation for
checking duplicated key in a table?

Here is my problem?

Usually I let users Insert/Modify in the textEdit and click OK button when
they are done
I code in the "cmdOKClick" event handler using  Try..Except..End; to trap
for it Key violation error and it all work fine
However, my current application I let users Insert/Modify record
right in the DBGrid link directly to a table,
I code in the onBeforePost to check for Duplicate key,
I get the error "Stack overflow" because it stay in the loop until it
exhausted.

Thanks you all

Tin Le

 

Re:Check Duplicated Key (Key Violation)


Check the Delphi help for OnPostError.

I can't remember exactly how I did this (the code is at home), but I know
there is a TI that talks about trapping the error for a keyviolation error.
 So, put code in OnPostError, look for a keyviolation error, and if so, set
the Tdata action to abort, fail, or retry, which ever you prefer.

If you can't figure this out on your own, or someone doesn't post how to
trap for the error, email me at r...@advancedventures.com and I will look
for it at home.

Tin Le <le_ti...@email.msn.com> wrote in article
<6p21qs$k...@forums.borland.com>...

Quote
> Hello all!
> Would you please tell me what is the best method to implementation for
> checking duplicated key in a table?

> Here is my problem?

> Usually I let users Insert/Modify in the textEdit and click OK button
when
> they are done
> I code in the "cmdOKClick" event handler using  Try..Except..End; to trap
> for it Key violation error and it all work fine
> However, my current application I let users Insert/Modify record
> right in the DBGrid link directly to a table,
> I code in the onBeforePost to check for Duplicate key,
> I get the error "Stack overflow" because it stay in the loop until it
> exhausted.

> Thanks you all

> Tin Le

Re:Check Duplicated Key (Key Violation)


If you want to check for a duplicate key in the BeforePost event use a
second TTable to search for the key value.

--
Bill

(TeamB cannot answer questions received via email.)
(To contact me for any other reason remove nospam from my address)

Re:Check Duplicated Key (Key Violation)


This may not be what you want, but this is what I do.  If you do this, you
do not need to check for a key violation.   You can use any data control
that you want as long as you put this in the OnPostError Event.  You do not
need to check for a key violation because the table will check for it
itself.  If a key violation occurs, this will cause the event cancel the
post. This is essentially how I did it. . . .

add BDE to your uses clause

var
KVError : boolean  // this is a variable of the datamodule or form.

procedure.btnPostClick
begin
KVError = false; // set KVerror to false
tblWebPerson.post; // Post the record
if KVerror then // if the kverror is true show the user what the problem is
  showmessage('there is a key violation')
end;

procedure TwmRossWebMusic.tblWebPersonPostError(DataSet: TDataSet;
  E: EDatabaseError; var Action: TDataAction);
begin
 if E is EDBEngineError then
    with EDBEngineError(E) do begin
         if Errors[ErrorCount - 1].ErrorCode = DBIERR_KEYVIOL then begin
            Action := daAbort;
            KVError := true;
         end;
    end;
end;

You can actually put the message in the OnPostError event, but I didn't
because for me this is a web application and can't use showmessage.  In a
non-web application, you can also drop the KVerror variable.

I hope this is helpful.

Tin Le <le_ti...@email.msn.com> wrote in article
<6p21qs$k...@forums.borland.com>...

Quote
> Hello all!
> Would you please tell me what is the best method to implementation for
> checking duplicated key in a table?

> Here is my problem?

> Usually I let users Insert/Modify in the textEdit and click OK button
when
> they are done
> I code in the "cmdOKClick" event handler using  Try..Except..End; to trap
> for it Key violation error and it all work fine
> However, my current application I let users Insert/Modify record
> right in the DBGrid link directly to a table,
> I code in the onBeforePost to check for Duplicate key,
> I get the error "Stack overflow" because it stay in the loop until it
> exhausted.

> Thanks you all

> Tin Le

Re:Check Duplicated Key (Key Violation)


On Tue, 21 Jul 1998 16:24:47 -0700, "Bill Todd (TeamB)"

Quote
<bill_todd.nos...@compuserve.com> wrote:
>If you want to check for a duplicate key in the BeforePost event use a
>second TTable to search for the key value.

My solution is save the data first (using DBFields array) and then
findkey if it is duplicate, is not then repost the data and save

The benefit of this solution is you can use it in dBase database too
(no need for paradox primary index)

Good luck

Other Threads