Board index » delphi » Key violation - error message

Key violation - error message

Hi,

I want the user to get an error message when he tries to write an existing
key value in a field. So I placed a locate-statement in the on-validate
event for that table-field, to check if the field value exists. But when I'm
running the code I get 'stack overflow'-error. I'm using Delphi 5 and
Paradox-tables.

Thanks,

Tove

 

Re:Key violation - error message


Tove,
  the user will get a key violation error by default in this case, unless
you trap the exception.  If you just want a nicer message the "Key
violation", then trap the key violation exception in your code, and display
or raise your own exception message instead.  (Calling Locate changes the
cursor location, which probably posts your current data, which calls
OnValidate again, which eventually blows the stack.)
-Howard

Quote
Tove <tove.bjornb...@c2i.net> wrote in message

news:8jphfq$cjs3@bornews.borland.com...
Quote
> Hi,

> I want the user to get an error message when he tries to write an existing
> key value in a field. So I placed a locate-statement in the on-validate
> event for that table-field, to check if the field value exists. But when
I'm
> running the code I get 'stack overflow'-error. I'm using Delphi 5 and
> Paradox-tables.

> Thanks,

> Tove

Re:Key violation - error message


Do not use locate in a event because after every operation in the table this
event will be executed and the you have a recursive execusion of the
procedure without an end. That's the reason because you have a stack
overflow. If you like to check the users input, use a component which is not
connected to a datasource, for example a TEdit. After a confirmation of the
user with a button or an other event you can use locate to check the values.

I almost never use components which are connected to a datasource to get
userinputs because it's very difficult to check the inputs.

"Tove" <tove.bjornb...@c2i.net> schrieb im Newsbeitrag
news:8jphfq$cjs3@bornews.borland.com...

Quote
> Hi,

> I want the user to get an error message when he tries to write an existing
> key value in a field. So I placed a locate-statement in the on-validate
> event for that table-field, to check if the field value exists. But when
I'm
> running the code I get 'stack overflow'-error. I'm using Delphi 5 and
> Paradox-tables.

> Thanks,

> Tove

Re:Key violation - error message


You have to use a second TTable for the Locate. When you call Locate and the
record is found Locate tries to move you to that record. Trying to move off
of the current record fires OnValidate which calls Locate which fires
OnValidate which calls Locate which fires OnValidate...

--
Bill Todd (TeamB)
(Questions received via email cannot be answered.)

Re:Key violation - error message


Is this a common way of checking for key violation, or is there a better
solution?

Tove

"Bill Todd (TeamB)" <b...@dbginc.com> wrote in message
news:8jr2uc$jv18@bornews.borland.com...

Quote
> You have to use a second TTable for the Locate. When you call Locate and
the
> record is found Locate tries to move you to that record. Trying to move
off
> of the current record fires OnValidate which calls Locate which fires
> OnValidate which calls Locate which fires OnValidate...

> --
> Bill Todd (TeamB)
> (Questions received via email cannot be answered.)

Re:Key violation - error message


How do I trap the key violation exception?

Thanks,
Tove

Re:Key violation - error message


On Tue, 4 Jul 2000 10:18:02 +0200, "Tove" <tove.bjornb...@c2i.net>
wrote:

Quote
>How do I trap the key violation exception?

Try this in the OnPostError event:

procedure Table1PostError(DataSet: TDataSet; E: EDatabaseError;
  var Action: TDataAction);
var
    ErrCode: DBIResult;
begin
    ErrCode := 0;
    if E is EDBEngineError then
        with EDBEngineError(E) do
            ErrCode := Errors[ErrorCount-1].ErrorCode;
    if ErrCode = DBIERR_KEYVIOL then
    begin
       <signal the problem and abort/retry>
    end else
        Action := daFail;
end;

HTH,

Jan

Re:Key violation - error message


If you need to know about the key violation as soon as the user moves off of
the primary key field in the form then using a second TTable to search for
the value is a good way. If you can wait until the user tries to post the
record then use the OnPostError event to detect the key violation.

--
Bill Todd (TeamB)
(Questions received via email cannot be answered.)

Other Threads