编辑:我想知道,在什么这是考虑到该过程的if..then..else
和DataSet
的二手MYTABLE
之间的区别? MYTABLE
是什么类型的对象?
看你当前的代码我假定以下(参见代码中的注释)
procedure TDataModule2.MYTABLEBeforePost(DataSet: TDataSet);
begin
if (MYTABLE.FieldByName('DONE').Value = 1) then //Checkbox checked
begin
//Should be done when checkbox checked and DONE_BY is not empty
DataSet.FieldByName('DONE_WHEN').AsDateTime:=Now;
end
else
begin
//Should happen if input doesn't meet conditions
DataSet['DONE_BY'] :='';
DataSet.FieldByName('DONE_WHEN').Clear;
end;
end;
如果我的假设是正确的这应该修复它:
procedure TDataModule2.MYTABLEBeforePost(DataSet: TDataSet);
begin
if (MYTABLE.FieldByName('DONE').Value = 1) and
(MYTABLE.FieldByName('DONE_BY').Value <> '') then //Checkbox checked and Done_by not empty
begin
//Should be done when checkbox checked and DONE_BY is not empty
DataSet.FieldByName('DONE_WHEN').AsDateTime:=Now;
end
else
begin
//Should happen if input doesn't meet conditions
DataSet['DONE_BY'] :='';
DataSet.FieldByName('DONE_WHEN').Clear;
Abort;
end;
end;
我相信Abort;
是你需要在Else
部分,但我不是100%确定的。
我希望这有助于你以任何方式,如果我误解你的问题让我知道:)
编辑: 鉴于你的最新评论,我认为以下应该解决这个问题。
procedure TDataModule2.MYTABLEBeforePost(DataSet: TDataSet);
begin
if (MYTABLE.FieldByName('DONE').Value = 1) then //Checkbox checked
begin
//Should be done when checkbox checked and DONE_BY is not empty
If DataSet['DONE_BY'] <> '' then
DataSet.FieldByName('DONE_WHEN').AsDateTime:=Now
else
Raise Exception.Create('Your message')
end
else
begin
//Should happen if input doesn't meet conditions
DataSet['DONE_BY'] :='';
DataSet.FieldByName('DONE_WHEN').Clear;
Raise Exception.Create('Your message')
end;
end;
您可以使用中止而不是异常。编辑到你自己的口味:)
放弃'中止;'在你的BeforePost,以防止更新 – 2014-09-02 01:11:05