以下是我的一个按钮事件:
procedure TfrmEnterDepot.cmdSaveClick(Sender: TObject);
begin
if cmdIndex>0 then
begin
if EnterDepotDetailTableEnterNum.Value=0 then
begin
ShowMessage('请输入入库明细。');
DBGrid1.SetFocus;
Abort;
end;
if StrToFloat(DBEdit8.Text)+StrToFloat(DBEdit9.Text)<StrToFloat(DBEdit10.Text) then
begin
ShowMessage('“入库费”与“杂费”之和不能小于“实收金额”。');
DBEdit10.SetFocus;
DBEdit10.Text:=FloatToStr(StrToFloat(DBEdit8.Text)+StrToFloat(DBEdit9.Text));
Abort;
end;
end;
try
if not frmMain.DataBase1.InTransaction then
frmMain.DataBase1.StartTransaction;
//刷新入库主表
EnterDepotTable.Edit;
if((StrToFloat(DBEdit8.Text)+StrToFloat(DBEdit9.Text))=StrToFloat(DBEdit10.Text))then
if Application.MessageBox('费用已经交清,是否将该单标记为“已经结算”?'+Chr(13)+Chr(13)+Chr(13)+'标记为“已经结算”后,将不能进行修改‘删除。','结算确认',MB_YESNO+MB_ICONQUESTION+MB_DEFBUTTON1)=IDYES then
EnterDepotTableBalance.Value:=True;
EnterDepotTable.Post;
EnterDepotTable.ApplyUpdates;
EnterDepotTable.CommitUpdates;
//刷新入库明细表
EnterDepotDetailTable.Edit;
EnterDepotDetailTable.Post;
EnterDepotDetailTable.ApplyUpdates;
EnterDepotDetailTable.CommitUpdates;
//刷新商品库存
if cmdIndex>0 then
begin
EnterDepotDetailTable.Filter:='EnterID='+DBEdit1.Text;
EnterDepotDetailTable.First;
While not EnterDepotDetailTable.Eof do
begin
LookWareTable.Locate('WareID',EnterDepotDetailTableWareID.Value,[]);
LookWareTable.Edit;
LookWareTableWareStock.Value:=(LookWareTableWareStock.Value+EnterDepotDetailTableEnterNum.Value);
EnterDepotDetailTable.Next;
end;
EnterDepotDetailTable.Filter:='';
end;
LookWareTable.Edit;
LookWareTable.Post;
LookWareTable.ApplyUpdates;
LookWareTable.CommitUpdates;
//提交事务
frmMain.DataBase1.Commit;
except
ShowMessage('输入错误,同一入库单中不能有相同的商品。请重新或取消输入。');
frmMain.DataBase1.RollBack;
Abort;
end;
TF:=True;
cmdIndex:=0;
Timer1.Enabled:=False;
ComonButtonClick(Sender);
EnterDepotTable.CachedUpdates:=False;
EnterDepotDetailTable.CachedUpdates:=False;
LookWareTable.CachedUpdates:=False;
//以上是对各个表的操作,如果成功,则记入“事务表”
if cmdIndex=1 then//cmdIndex=1表示是添加新的记录
OperateLogTable.InsertRecord([PersonID, Date, AddrEdit.Text, Null, Null, Null, Null, Null, Null, DiscountEdit.Text]);
PersonID,OperationDate,'frmEnterDepot.cmdSaveClick','EnterDepot,EnterDepotDetail,InfoWare',0,False);
//0=Insert,1=Modify,2=Delete,
else if cmdIndex=2//cmdIndex=2表示是修改记录
.....
else if cmdIndex=3//cmdIndex=3表示是删除记录
.....//
end;
cmdCancel按钮中则是对放弃的操作记录,cmdIndex=2/cmdIndex=3的代码很长,因为要判断
在修改过程中,是否删除了子表记录,是否添加了新的子表记录。