Duplicate Error on insert record 
Author Message
 Duplicate Error on insert record

When I insert a record which has a duplicated key my program crashes.

I know that I can solve this problem by using the TBatchMove methode,
but I do not know how to use it.

Can somebody help me with this problem

Thanks

Rob Huijser



Mon, 01 Mar 1999 03:00:00 GMT  
 Duplicate Error on insert record

Quote:

>When I insert a record which has a duplicated key my program crashes.

>I know that I can solve this problem by using the TBatchMove methode,
>but I do not know how to use it.

>Can somebody help me with this problem

>Thanks

>Rob Huijser

The problem with duplicate keys is fairly complex - depending on what
you are trying to do and whether you are using  Delph 1.0 or 2.0.
TBatchMove in itself is not necessarily the answer. One way to avoid
dupes is to use 2 tables. In Table1  BeforePost event you look for the
key in Table2 if it doesn't exist then it is safe to post. Of course
you have to post to both tables. I use this method for small files
maybe 1000 records. To avoid using disk space, I create a Table 2
(using Desktop Database)  that contains ONLY the key fields of Table1.

At Design time I set the TbatchMove source = Table1, TbatchMove
destination = Table2, TbatchMove Mode = batAppendUpdate and execute
the TbatchMove in the FormActivat event. This results in Table 2
contain ALL of the key fields that are in Table1. When the form is
closed I empty Table2 . This sample works in both 1.0 and 2.0. You can
modify it to check for a key violation before any posting takes place
(e.g. after user enteres the key field value) by putting the code in
the BeforePost event into a procedure that sets a flag true only if
there is no duplicate - this can save user from entering more data
only to lose it because of the key violation. I suggest you do some
time tests with this method - write a little program that will
generate as many records as you think might ever be in Table1 and see
if the time constraints work for you. Also bear in mind that you are
using extra disk space when you TBatchMove data to Table2.

(Item is the name of the primary key field in the Table)

procedure TForm1.Table1BeforePost(DataSet: TDataset);
begin
If Table2.Findkey ([Table1Item.Value])then
begin
   showmessage ('already exists');
   TABLE1.CANCEL;
   ABORT; {note that this halts program ONLY when running it within
Delphi - if user runs it there is no halt}
end
else begin
   showmessage ('ok to post');
   If Table1.State = dsInsert then Table2.Insert;
   If Table1.State = dsEdit then Table2.Edit;
   Table2Item.Value := Table1Item.Value;
   Table2.Post;
end;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
With Table2 do
   table2.Active := False;      {Close the table}
   DatabaseName := 'VACATION';
   TableName := 'TOTAKEX.db';
   TableType := ttParadox;
   EmptyTable;
end;
end;

I am continuing to study this aspect of Delphi and am working toward
finding more efficient methods - you can e-mail me at

if you have more questions or need more help. If you do, please let me
know what version of Delphi you are using.

Note also that there are other recent postings to the group that offer
other methods - you might check them out as they might better serve
your needs. Delphi knows I am no expert on this!!!!

Good luck

Kip Winsett



Tue, 02 Mar 1999 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Errors while inserting records

2. trapping key violation error when inserting new record using SQL

3. Duplicate key inserted with dBase Unique index

4. Duplicates after insert (Problem with update objects)

5. dBase Unique indexes: No failure on duplicate insert

6. copying data from one record to the next when inserting a new record

7. duplicate records in join

8. Handling Duplicate records

9. Using SQL to delete duplicate records

10. Duplicate records

11. Tracking duplicate records?

12. Duplicate records in Interbase

 

 
Powered by phpBB® Forum Software