Recent

Author Topic: Best option dbgrid and dataset sql validate if record exist  (Read 833 times)

eldonfsr

  • Sr. Member
  • ****
  • Posts: 456
Hello friend is my first time i need to control a insert, edit, delete on dbgrid with register from tsquery

what is best event to validate if record exist when is new record on tsquery event onbeforepost

procedure TDM.ZTDocsBeforePost(DataSet: TDataSet);
begin
  if( not  ZTDocs.Locate('model ; serie', VarArrayOf([DM.ZTDocs.FieldByName('model').AsString ,DM.ZTDocs.FieldByName('serie').AsString ]), [loCaseInsensitive,loPartialKey]))  then begin
    ShowMessage('Register with this values already registered');
  end;
end;   

or

procedure TDM.ZQMDocBeforePost(DataSet: TDataSet);
Var bm:TBookmark;
  Modl,ser:String;
begin
  bm:=DataSet.Bookmark;
  Modl:= Dataset.FieldByName('Model').AsString;
  Ser:= DataSet.FieldByName('serie').AsString ;
  if( ZQMDoc.Locate('model ; serie', VarArrayOf([Modl ,Ser ]), [loCaseInsensitive,loPartialKey]) ) then begin
     Showmessage('Reister with values already exist');
     ZQMDoc.Cancel;
  end else begin
     ZQMDoc.Post;
     ZQMDoc.GotoBookmark(bm);
  end;
end;     

what is better option or some example ro control database error..... or avoid the app crash...


 

Hansvb

  • Hero Member
  • *****
  • Posts: 632

egsuh

  • Hero Member
  • *****
  • Posts: 1381
Re: Best option dbgrid and dataset sql validate if record exist
« Reply #2 on: June 09, 2024, 08:40:46 am »
If you are entering only model and serie, they you may use try.. except clause directly. This assume that there defined unique key with model and serie. So, this tries to raise exception.

Code: Pascal  [Select][+][-]
  1. try
  2.      ZQMDoc.Post;
  3. except
  4.      ZQMDoc.Abort;
  5. end;

But if you try to enter model/serie first and there are other fields to enter, I think you'd better enter model/serie first, and if successful then let other fields be edited (inserted, practically).

 

TinyPortal © 2005-2018