Declare a HandleProductSelection
method in your form (or datamodule):
procedure HandleProductSelection(Sender: TField);
Attach an AfterOpen
handler to the OrderTable. In that event handler, find the Product code field and attach the HandleProductSelection method to the field's OnChange event.
procedure TForm1.OrderTableAfterOpen(DataSet: TDataSet);
var
Field: TField;
begin
Field := OrderTable.FindField('PCode');
Field.OnChange := HandleProductSelection;
end;
This ensures that whenever your OrderTable is opened, the event handler will be attached to the proper field and that in turn will ensure that the HandleProductSelection method will be called whenever the contents of the Product Code field are changed.
Implement the HandleProductSelection method. If you defined the lookup field for the product code using the field editor, you have a Product dataset on your form (or datamodule). The dataset with the product information will then be positioned according to the value of the Product code field in the current record of your OrderTable.
You can take advantage of this in your HandleProductSelection method by simply transferring the information from the Product dataset to the OrderTable:
procedure TForm1.HandleProductSelection(Sender: TField);
begin
OrderTable.FieldByName('PPrice').AsCurrency :=
DataSetProduct.FieldByName('PPrice').AsCurrency;
end;