HELP: Key Violation Error

Hi all:

I have two paradox tables, 'invoice master' and 'invoice detail', the
master table's primary key is 'INVNO', and the detail table's primary
key is 'INVNO;ITEMNO', both table CachedUpdate is set to TRUE.

I'm trying to renum the detail table ITEMNO field each time I
add/delete the record in the detail table, for new insert record
(master), every thing is ok, but if I edit the master and add/delete
some record to/from detail table I get 'Key Violation' error, this
error will only occur when I do the insertion and deletion on the
detail table at the same time ! don't know what I'm doing wrong....

here is the code :

procedure TForm1.tblMasterAfterCancel(DataSet: TDataSet);
begin
  tblDetail.CancelUpdates;
  tblMaster.CancelUpdates;
end;

procedure TForm1.tblMasterAfterDelete(DataSet: TDataSet);
begin
  (DataSet as TTable).Database.ApplyUpdates( [tblDetail, tblMaster] );
end;

procedure TForm1.tblMasterAfterPost(DataSet: TDataSet);
begin
  (DataSet as TTable).Database.ApplyUpdates( [tblMaster, tblDetail] );
end;

procedure TForm1.tblMasterBeforeInsert(DataSet: TDataSet);
begin
  iItemNo := 1;
end;

procedure TForm1.tblMasterBeforeEdit(DataSet: TDataSet);
begin
  with tblDetail do begin
    Last;
    iItemNo := tblDetailItemNo.Value + 1;
    First;
  end;
end;

procedure TForm1.tblMasterBeforePost(DataSet: TDataSet);
begin
  RenumItemNo;
end;

procedure TForm1.tblDetailNewRecord(DataSet: TDataSet);
begin
  tblDetailItemNo.Value := iItemNo;
end;

procedure TForm1.tblDetailAfterPost(DataSet: TDataSet);
begin
  Inc(iItemNo);
end;

procedure TForm1.RenumItemNo;
var
  i: Integer;
  TheBookMark: TBookMark;
  pBeforeEdit, pAfterEdit, pBeforePost, pAfterPost,
  pBeforeScroll, pAfterScroll, pOnCalcFields: TDataSetNotifyEvent;
begin
  with tblDetail do begin
    TheBookMark := GetBookMark;
    DisableControls;
    // Save old dataset event
    pBeforeEdit   := BeforeEdit;     BeforeEdit   := nil;
    pAfterEdit    := AfterEdit;      AfterEdit    := nil;
    pBeforePost   := BeforePost;     BeforePost   := nil;
    pAfterPost    := AfterPost;      AfterPost    := nil;
    pBeforeScroll := BeforeScroll;   BeforeScroll := nil;
    pAfterScroll  := AfterScroll;    AfterScroll  := nil;
    pOnCalcFields := OnCalcFields;   OnCalcFields := nil;

    try
      i := 1;
      First;
      while not Eof do begin
        if tblDetailItemNo.Value <> i then begin
           Edit;
           tblDetailItemNo.Value := i;
           Post;
        end;
        Inc( i );
        Next;
      end;
      iItemNo := i;
    finally
      if TheBookMark <> nil then begin
         GotoBookMark( TheBookMark );
         FreeBookMark( TheBookMark );
      end;
      // Restore dataset event
      BeforeEdit   := pBeforeEdit;
      AfterEdit    := pAfterEdit ;
      BeforePost   := pBeforePost;
      AfterPost    := pAfterPost ;
      BeforeScroll := pBeforeScroll;
      AfterScroll  := pAfterScroll;
      OnCalcFields := pOnCalcFields;
      EnableControls;
    end;
  end;
end;

Please Help, Thanks
J.C.Voon