Forum > Databases

Best option dbgrid and dataset sql validate if record exist

(1/1)

eldonfsr:
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:
Perhaps this is what you could use


--- Quote --- https://lazarus-ccr.sourceforge.io/docs/lcl/controls/tcontrol.oneditingdone.html
--- End quote ---

egsuh:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---try     ZQMDoc.Post;except      ZQMDoc.Abort; 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).

Navigation

[0] Message Index

Go to full version