0

I have two datamodules in an application: TPAymentDataModule and TJobEditDataModule. In one DataModule I have to invoke a certain TADOQuery from the other module to update the database: (This piece of code is in 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;

The OrderEditQuery's Order_status_id field has an OnValidate event handler:

(This piece of code is in another module: 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;

All seems to work correctly, but when the OnValidate handler is hit, all fields suddenly have default values and cause the code to fail. I don't Create the queries and use them as singletons, so I expect that Sender in the event handler should point to the same instance of the query, since there is only one singleton query available.

But I am new to Delphi and so keep wondering, what is going wrong here?

AunAun
  • 1,423
  • 2
  • 14
  • 25

1 Answers1

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;
Graymatter
  • 6,529
  • 2
  • 30
  • 50
  • Thanks Graymatter, I forgot to put this line in. But the problem is the code didn't even reach it because of validation failure in the other module. Oddly, when I moved the `ForceOrdersToComplete` method into `JobEditDataModule`, validation started working correctly. Seems that references to fields got lost between datamodules? – AunAun Jun 06 '14 at 09:25
  • @user3243551: please include ALL details in your question, these things matter. Failing to do so will result in wasting someone else's time. Try to make an SSCCE (shorten your code in such a way that it still exhibits the problem and put that in your question) – whosrdaddy Jun 06 '14 at 09:31