Delphi:在另一个模块中调用的TADOQuery字段突然有默认值

时间:2014-06-06 07:39:28

标签: class delphi tadoquery

我在应用程序中有两个数据模块:TPAymentDataModule和TJobEditDataModule。在一个DataModule中,我必须从另一个模块调用某个TADOQuery来更新数据库: (这段代码在TPaymentDataModule中):

procedure TPaymentDataModule.ForceOrdersToComplete;
begin
  With PaymentGroupContentQuery do
  begin
    if IsEmpty then exit;
    First;
    JobEditDataModule.OrderEditQuery.Active:=true;
    JobEditDataModule.JobEditQuery.Active := true;

    while not eof do
    begin

      // get all orders that refer to this payment group 
      JobEditDataModule.OrderEditQuery.Parameters.ParamValues['orderId'] :=
      PaymentGroupContentQueryorder_Id.Value;
      JobEditDataModule.OrderEditQuery.Active:=true;
      JobEditDataModule.OrderEditQuery.Edit;

      // and try to modify their status
      JobEditDataModule.OrderEditQueryorder_status_id.AsInteger := 2;
     Next;
   end;
 end;
end;

OrderEditQuery的{​​{1}}字段有一个Order_status_id事件处理程序:

(这段代码在另一个模块中:TJobEditDataModule):

OnValidate

所有似乎都正常工作,但是当procedure TJobEditDataModule.OrderEditQueryorder_status_idValidate(Sender: TField); begin if (not Self.IsNewJobStatusPermissible(OrderEditQueryorder_status_id.OldValue, OrderEditQueryorder_status_id.NewValue)) then begin raise Exception.CreateFmt('Cannot modify status of order %s.', [ OrderEditQueryOrder_Name.AsString ]); Abort; end; end; 处理程序被命中时,所有字段突然都有默认值并导致代码失败。 我没有OnValidate查询并将它们用作单例,所以我希望事件处理程序中的Create应该指向同一个查询实例,因为只有一个单例查询可用。 / p>

但我是Delphi的新手,所以一直在想,这里出了什么问题?

1 个答案:

答案 0 :(得分:1)

如果我没有记错,您需要Post您对DB的编辑:

procedure TPaymentDataModule.ForceOrdersToComplete;
begin
  ..
  while not eof do
    begin
      // get all orders that refer to this payment group 
      JobEditDataModule.OrderEditQuery.Parameters.ParamValues['orderId'] :=
      PaymentGroupContentQueryorder_Id.Value;
      JobEditDataModule.OrderEditQuery.Active:=true;
      JobEditDataModule.OrderEditQuery.Edit;

      // and try to modify their status
      JobEditDataModule.OrderEditQueryorder_status_id.AsInteger := 2;

      // Commit the changes to the DB here
      JobEditDataModule.OrderEditQuery.Post;

      Next;
   end;
end;