Autopopulate subform based on other subform inputs

Autopopulate subform based on other subform inputs

Hello, I have the following forms and subform:


Purchase Order Form

Subform X (items in packages)

Subform Y (Items)

Total



SubForm_X (items in packages)

PackagesNameX (lookup field from Packages_Form.PackageName_p)


Subform_Y (Items)

(*)Material_Y

(#)Unit_Y

(#)Price_Y

Quantity_Y

Subtotal_Y


————


Materials_Form

Material_M

(#)Unit_M

(#)Price_M


Packages_Form 

PackageName_p

Subform Z:

(*)Material_P

(#)Unit_P

Quantity_P


(*) Is a lookup field to Material_M in Materials_Form.

(#) Fetched from Materials_Form


What I am looking to do is auto populate the Subform Y (Items) based on the Subform X inputs, fetch the units and price, and if there are 2 of more equal materials, sum them. For example:


Packages_Form 
PackageName_pPackage A
(*)Material_P(#)Unit_PQuantity_P
CementTON1
DrywallPIECE10
PipelinePIECE20
Packages_Form 
PackageName_pPackage B
(*)Material_P(#)Unit_PQuantity_P
CementTON2
DrywallMETER5
BrickPIECE25
Purchase Order Form
Subform X
PackagesNameX
Package A
Package B
Package B
Subform Y
(*)Material_Y(#)Unit_Y(#)Price_YQuantity_YSubtotal_Y
CementTON10550
DrywallPIECE1520300
PipelineMETER1720340
BrickPIECE250100

Total: $790

The Subtotal_Y and Total are alredy set up with:

  1. if(row.Material_Y != null)
  2. {
  3. row.Subtotal_Y=row.Price_Y * row.Quantity_Y;
  4. }
  5. x = 0.0;
  6. if(row.Price_Y != null)
  7. {
  8. for each  rec in Subform_Y
  9. {
  10. x = x + rec.Subtotal_Y;
  11. }
  12. }
  13. input.Total = x;

thanks!