Script lengkap tambah edit hapus

unit UtemplateMaster;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, UTemplate, Vcl.StdCtrls, Data.DB,
  ZAbstractRODataset, ZAbstractDataset, ZDataset, ZSqlUpdate,Zconnection;

type
  TFrmTemplateMaster = class(TFrmTemplate)
    zqrymaster: TZQuery;
    btnsimpan: TButton;
    btnbatal: TButton;
    btntambah: TButton;
    btnedit: TButton;
    btnhapus: TButton;
    zupdsql: TZUpdateSQL;
    procedure btntambahClick(Sender: TObject);
    procedure btneditClick(Sender: TObject);
    procedure btnhapusClick(Sender: TObject);
    procedure btnbatalClick(Sender: TObject);
    procedure btnsimpanClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure zupdsqlAfterInsertSQLStatement(const Sender: TObject;
      StatementIndex: Integer; out UpdateAutoIncFields: Boolean);
  private
    { Private declarations }
    FId:Integer;
  protected
  procedure setEnabledSave(isEnabled:boolean);virtual;
  procedure doAfterApplyUpdates;virtual;
  public
    { Public declarations }

  end;

var
  FrmTemplateMaster: TFrmTemplateMaster;

implementation

{$R *.dfm}
procedure TFrmTemplateMaster.doAfterApplyUpdates;
begin

end;
procedure TFrmTemplateMaster.btnbatalClick(Sender: TObject);
begin
  inherited;
zqrymaster.CancelUpdates;
SetEnabledSave(false);
end;

procedure TFrmTemplateMaster.btneditClick(Sender: TObject);
begin
  inherited;
zqrymaster.Edit;
setenabledSave(true);
end;

procedure TFrmTemplateMaster.btnhapusClick(Sender: TObject);
var cn:Tzconnection;
begin
  inherited;
  cn:=Tzconnection(zqrymaster.Connection);
  if not cn.InTransaction then
  cn.StartTransaction;
        try
        zqrymaster.Delete;
        zqrymaster.ApplyUpdates;

        cn.Commit;
         zqrymaster.CommitUpdates;
        except
          on E:exception do
          begin
            cn.Rollback;
            messagedlg(e.Message,mterror,[mbok],0);
            zqrymaster.CancelUpdates;
          end;

        end;

end;

procedure TFrmTemplateMaster.btnsimpanClick(Sender: TObject);
var cn:Tzconnection;
begin
  inherited;
  cn:=Tzconnection(zqrymaster.Connection);
  if zqrymaster.state in [dsInsert,dsedit] then
  zqrymaster.post;
  if not cn.InTransaction then
  cn.StartTransaction;
  try
    zqrymaster.ApplyUpdates;
     doAfterApplyUpdates;
      if zqrymaster.Fields[0].IsNull then
       begin
         zqrymaster.Edit;
         zqrymaster.Fields[0].Value:=Fid;
         zqrymaster.Post;


       end;
    cn.Commit;


        zqrymaster.CommitUpdates;
  except
    on E:exception do
    begin
         cn.Rollback;
         messagedlg(e.Message,mterror,[mbok],0);
    end;

  end;

end;

procedure TFrmTemplateMaster.btntambahClick(Sender: TObject);
begin
  inherited;
zqrymaster.Append;
SetEnabledSave(true);
end;

procedure TFrmTemplateMaster.FormCreate(Sender: TObject);
begin
  inherited;
zqrymaster.SortedFields:=zqrymaster.Fields[0].FieldName;

end;

procedure TfrmTemplateMaster.setEnabledSave(isEnabled: Boolean);
begin
btnsimpan.enabled:=isEnabled;
btnbatal.enabled:=isEnabled;
btntambah.enabled:=isEnabled;
btnedit.enabled:=isEnabled;
btnhapus.enabled:=isEnabled;
end;
procedure TFrmTemplateMaster.zupdsqlAfterInsertSQLStatement(
  const Sender: TObject; StatementIndex: Integer;
  out UpdateAutoIncFields: Boolean);
begin
  inherited;
if StatementIndex=0 then
begin
  Fid:=getLastId;
end;
end;

end.