Jass Saini
asked on
Adding fields on a Subform and getting results on the Mainform
I am inputting numbers on the subform but want the results on the main form..how do I do that?
ASKER
I am still unclear. I would like the sum of the field from the subform in a textbox on the mainform
I am inputting numbers on the subform...
It seems your are saying, your subform is continuous type where the values in a certain field can be edited. Based on this assumption you can try the following.
Let's say the name of this text box in the subform is txtValuesToEdit (with matching field name of ValuesToEdit in its table). From the Private Sub Form_AfterUpdate() event of the subform fire UpdateSum shown below (assuming the main form is also has txtSum on it):
Also, add UpdateSum to Private Sub Form_Current() of the subform so that upon opening the form it would show the total amount.
Mike
It seems your are saying, your subform is continuous type where the values in a certain field can be edited. Based on this assumption you can try the following.
Let's say the name of this text box in the subform is txtValuesToEdit (with matching field name of ValuesToEdit in its table). From the Private Sub Form_AfterUpdate() event of the subform fire UpdateSum shown below (assuming the main form is also has txtSum on it):
Private Sub UpdateSum()
Dim rs As DAO.Recordset
Dim total As Variant
Set rs = Me.RecordsetClone
If rs.RecordCount > 0 Then
rs.MoveFirst
Do Until rs.EOF
total = total + Nz(rs!ValuesToEdit)
rs.MoveNext
Loop
End If
Parent!txtSum.SetFocus
Parent!txtSum.Text = total
End Sub
Private Sub Form_AfterUpdate()
UpdateSum
End Sub
Private Sub Form_Current()
UpdateSum
End Sub
Also, add UpdateSum to Private Sub Form_Current() of the subform so that upon opening the form it would show the total amount.
Mike
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Pat,
ControlSource = SubformName!txtSum was the key I missed.
First I started using sum in the subform footer pushing it to the parent form but there was sort the delaly (it showed older sum not the most recent).
With your solution, I think there is a need to have Parent.Refresh in the after update event of text box being edited. hopefully it will work then.
It is possible, your solution also have the delay problem.
Mike
ControlSource = SubformName!txtSum was the key I missed.
First I started using sum in the subform footer pushing it to the parent form but there was sort the delaly (it showed older sum not the most recent).
With your solution, I think there is a need to have Parent.Refresh in the after update event of text box being edited. hopefully it will work then.
It is possible, your solution also have the delay problem.
Mike
ASKER
Thanks!
This is a good reference for the subject.
The one you want is:
=Me!Subform1.Form!ControlN
You can refer to totals in the footer of the subform using this method, or you can have a button on the subform that does something like
me.parent.requery
if the subform is bound to data that the main form is also based on.