unexpected delete key/record

unexpected delete key/record

Post by Henrik Mou Martins » Tue, 17 Oct 1995 04:00:00



In my programe I want to test for key violations (duplicate keys).

To do so I use a Try..Except block.

All fields on the form is data bound.
The exception for key violations is traped in the try..except block,
but
some how the record buffer for the new record is deleted !! WHY ???

This means that I am not able to update the record with the values
that
the user has typed into the fields, because I need to call
Table1.cancel
to get a new record buffer. Once I call Table1.Cancel, the records is
automaticaly cleared and the user will loose all entries in the
databound
fields.

Is there a solution to this problem ???

{************************************************************************}

{CLICK ON BUTTON1 TO OPEN DATABASE AND PUT INTO INSERT MODE}
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
  Table1.Open; {OPEN TABLE}
  Table1.Insert; {PUT INTO INSERT MODE}
end;

{CLICK ON BUTTON2 TO TRY POSTING RECORD}
procedure TForm1.BitBtn1Click(Sender: TObject);
var
  i : integer;
begin
  try            {TRY..}
    Table1.Post; {POSTING THIS RECORD}
  except
    on E : EDBEngineError do {ON ERROR :...}
    begin
      for i := 0 to E.ErrorCount - 1 do {LOOK THROUGH ERRORS IN
DB-ENGINE}
      begin
        case e.errors[i].errorcode of
          DBIERR_KEYVIOL : showmessage ('KEY VIOLATION'); {DUPLICATE
KEY}
        else
          showmessage (e.errors[i].message); {ELSE SHOW ERROR MESSAGE}
        end;
      end;
    end;
  end;
end;

{************************************************************************}

 
 
 

unexpected delete key/record

Post by Steve Koters » Tue, 17 Oct 1995 04:00:00



Quote:>In my programe I want to test for key violations (duplicate keys).

>To do so I use a Try..Except block.

>All fields on the form is data bound.
>The exception for key violations is traped in the try..except block,
>but some how the record buffer for the new record is deleted !! WHY ???

>This means that I am not able to update the record with the values
>that the user has typed into the fields, because I need to call
>Table1.cancel to get a new record buffer. Once I call Table1.Cancel,
>the records is automaticaly cleared and the user will loose all entries
>in the databound fields.

>Is there a solution to this problem ???

Try calling the Abort procedure where you are now invoking the Cancel
method of the TTable component. This should raise a silent exception that
will halt the posting of the record, but leave the edit values intact.

**************************************************************************
Steve Koterski
Product Group Manager
Delphi Technical Support
Borland International, Inc.

 
 
 

unexpected delete key/record

Post by Lloy » Wed, 18 Oct 1995 04:00:00



Quote:>In my programe I want to test for key violations (duplicate keys).

>To do so I use a Try..Except block.

>All fields on the form is data bound.
>The exception for key violations is traped in the try..except block,
>but
>some how the record buffer for the new record is deleted !! WHY ???

>This means that I am not able to update the record with the values
>that
>the user has typed into the fields, because I need to call
>Table1.cancel
>to get a new record buffer. Once I call Table1.Cancel, the records is
>automaticaly cleared and the user will loose all entries in the
>databound
>fields.

>Is there a solution to this problem ???

>{************************************************************************}

>{CLICK ON BUTTON1 TO OPEN DATABASE AND PUT INTO INSERT MODE}
>procedure TForm1.BitBtn2Click(Sender: TObject);
>begin
>  Table1.Open; {OPEN TABLE}
>  Table1.Insert; {PUT INTO INSERT MODE}
>end;

>{CLICK ON BUTTON2 TO TRY POSTING RECORD}
>procedure TForm1.BitBtn1Click(Sender: TObject);
>var
>  i : integer;
>begin
>  try            {TRY..}
>    Table1.Post; {POSTING THIS RECORD}
>  except
>    on E : EDBEngineError do {ON ERROR :...}
>    begin
>      for i := 0 to E.ErrorCount - 1 do {LOOK THROUGH ERRORS IN
>DB-ENGINE}
>      begin
>        case e.errors[i].errorcode of
>          DBIERR_KEYVIOL : showmessage ('KEY VIOLATION'); {DUPLICATE
>KEY}
>        else
>          showmessage (e.errors[i].message); {ELSE SHOW ERROR MESSAGE}
>        end;
>      end;
>    end;
>  end;
>end;

>{************************************************************************}

It should work.  Try this:  (I used the customer table from
dbdemos.)

with table1 do begin
  insert;
  fields[0].AsInteger := 1221;
  try
    post;
  except
    on e: exception do
      if e.message = 'Key violation' then
        ShowMessage('There is a duplicate.');
  end;
end;

Lloyd <Borland>

    fields[0].AsInteger := 1221;
    try
        post;
    except
        on e: exception do
        if e.message = 'Key violation' then
                ShowMessage('There is a duplicate.');
    end;
  end;