Question : Calculate value on subform using field on main form

I have a main form called frmOrder with subform called frmOrderLine. The main form contains a number field which holds an exchange rate for the order (txtExchangeRate). On each record on the subform I have a unit price (txtUnitPrice) which is in our standard currency which is GBP, I want to have another text box on the subform for each line which shows the unit price in a different currency depending on the exchange rate entered on the main form. So if the exchange rate on the main form is changed to say 1.5, I want the text box on the sub form to display a value which is = 1.5 * txtUnitPrice.
When I change the exchange rate on the main form, all lines on the sub form need to update automatically.
Any ideas?

Answer : Calculate value on subform using field on main form

First, this would need to be a bound field on the subform, so you'd have to add a field to your table (or use a shaped recordset afor that form, assuming you're using 2000 or later).

After that, you'd just do an Update when the user changes the value in your mainform:

Sub YourTextbox_AfterUpdate()
  Currentproject.connection.Execute "UPDATE YourSubformTable SET YourSubformField= txtUnitPrice * " & Me.YourMainFormField " WHERE SomeIDValue=" & Me.SomeIDValue
  Me.YourSubfomrControl.Form.Requery
End Sub

The "SomeIdValue" is the field in the subform's recordsource that links those two datasets togther (i.e. it's the "Child" value in the Master/Child linkfields for the Subform control). Obviously you'd have to change the names of tables, fields etc to match your own.

Random Solutions  
 
programming4us programming4us