質問

I have database Paradox 7. Two tables:

Order
--------
OCode
ODate // Buy date
OCount // Product count
PPrice // Price of product in current moment
PCode // Product code
CCode //Client code


Product
---------
PCode
PName
PPrice 

On OrderTable(TTable) I add lookup field Product(relation PCode(Order)<->PCode(Product)) which returns PName. I need when I choose on DBGrid Product in field Order.PPrice store Product.PPrice

P.S>Sorry for my bad english.

役に立ちましたか?

解決

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;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top