0

我在应用程序中有两个数据模块: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事件处理程序:Order_status_idOnValidate

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

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查询并将它们用作单例,所以我希望Sender在事件处理程序中应该指向同一个查询实例,因为只有一个单例查询可用。

但我是德尔福的新手,所以一直想知道,这里出了什么问题?

4

1 回答 1

1

If I am not mistaken you need to Post your edit to the 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;
于 2014-06-06T09:05:06.590 回答