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?