Using the SUM function in a main form -referring to a sub form?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi guys.

I have a field in a sub form called Total Item Cost which multiplies the
quantity and price of an item in my ordering system. This form is called
Order Details Subform, then I have my main order form called Orders I want to
create a text box in this main form which SUMs the field Total Item Cost, can
this be done and if so how do I go about this?

Can you apply calculations from fields in a sub form to fields in a main
form? As I also have a discount multiplier in the mai form I would like to
multiply the SUM total by also. But obviously the answer to the first part of
my question should answer the second part.
 
1. From the database window, open your subform in design view.

2. If you do not see a Form Footer section, click Form Header/Footer on the
View menu. You can set the section's Visible property to No if you do not
wish to see this section.

3. In the Form Footer section, add a text box, and give it these properties:
Control Source =Sum([Quantity] * [Price])
Name txtSubTotal
Format Currency

4. Save and close.

5. In the main form, set the Control Source of the text box that should show
this total to:
=[Order Details Subform].[Form].[txtSubTotal]

You can then multiply that value by your Discount Multiplier field to
display the calculated total in another text box. (Note that you should not
store the amount of discount nor the order total in the main Order table.)
 
Allen,

Are there any rules or restrictions on the ordering of your calculations
(the ordering of your controls) in a continuous form used as a subform? The
reason for the question is due to receiving #Error messages when attempting
to do a sum of a control which is a calculated expression. For example, the
control is called Estimate and the value is calculated as [Est1] + [Est2] +
[Est3] + [Adj]. Summing the Estimate as =sum([Est1] + [Est2] + [Est3] +
[Adj]) produces #Error messages for this new control in addition to #Error
messages throughout the subform (even though it does not appear that other
controls are related in any way). Made me think maybe calcs need to occur in
a certain order.

Thank you,
Don

Allen Browne said:
1. From the database window, open your subform in design view.

2. If you do not see a Form Footer section, click Form Header/Footer on the
View menu. You can set the section's Visible property to No if you do not
wish to see this section.

3. In the Form Footer section, add a text box, and give it these properties:
Control Source =Sum([Quantity] * [Price])
Name txtSubTotal
Format Currency

4. Save and close.

5. In the main form, set the Control Source of the text box that should show
this total to:
=[Order Details Subform].[Form].[txtSubTotal]

You can then multiply that value by your Discount Multiplier field to
display the calculated total in another text box. (Note that you should not
store the amount of discount nor the order total in the main Order table.)

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Richard Horne said:
Hi guys.

I have a field in a sub form called Total Item Cost which multiplies the
quantity and price of an item in my ordering system. This form is called
Order Details Subform, then I have my main order form called Orders I want
to
create a text box in this main form which SUMs the field Total Item Cost,
can
this be done and if so how do I go about this?

Can you apply calculations from fields in a sub form to fields in a main
form? As I also have a discount multiplier in the mai form I would like to
multiply the SUM total by also. But obviously the answer to the first part
of
my question should answer the second part.
 
I am Noor, Your great solution proves really helpful. I faced the same
problem which is solved by your provided solution but there is a little
problem still present. I have two forms on main form. I have to calculate the
SUM of Quantity of each Item in both the subforms and subtract the total of
second subform from the first subform in main form. Your provided solution
help me to solve this problem but when the value in second form equal to
Zero, then it show 0 as answar while it should show the original value of the
first subform. Please help me. Thankx alot in advance.

Allen Browne said:
1. From the database window, open your subform in design view.

2. If you do not see a Form Footer section, click Form Header/Footer on the
View menu. You can set the section's Visible property to No if you do not
wish to see this section.

3. In the Form Footer section, add a text box, and give it these properties:
Control Source =Sum([Quantity] * [Price])
Name txtSubTotal
Format Currency

4. Save and close.

5. In the main form, set the Control Source of the text box that should show
this total to:
=[Order Details Subform].[Form].[txtSubTotal]

You can then multiply that value by your Discount Multiplier field to
display the calculated total in another text box. (Note that you should not
store the amount of discount nor the order total in the main Order table.)

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Richard Horne said:
Hi guys.

I have a field in a sub form called Total Item Cost which multiplies the
quantity and price of an item in my ordering system. This form is called
Order Details Subform, then I have my main order form called Orders I want
to
create a text box in this main form which SUMs the field Total Item Cost,
can
this be done and if so how do I go about this?

Can you apply calculations from fields in a sub form to fields in a main
form? As I also have a discount multiplier in the mai form I would like to
multiply the SUM total by also. But obviously the answer to the first part
of
my question should answer the second part.
 
Presumably you have two subforms (say Sub1 and Sub2), and you have the total
in the Form Footer section of each subform working correctly.

To subtract one from the other, place a text box on the main form, and set
its Control Source to an expression like this:
=Nz([Sub1].[Form].[txtTotal],0) - Nz([Sub2].[Form].[txtTotal],0)

That should work, unless the entire Detail section of a subform goes blank
due to this issue:
http://allenbrowne.com/casu-20.html

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.


Noor ul Hassan said:
I am Noor, Your great solution proves really helpful. I faced the same
problem which is solved by your provided solution but there is a little
problem still present. I have two forms on main form. I have to calculate
the
SUM of Quantity of each Item in both the subforms and subtract the total
of
second subform from the first subform in main form. Your provided solution
help me to solve this problem but when the value in second form equal to
Zero, then it show 0 as answar while it should show the original value of
the
first subform. Please help me. Thankx alot in advance.

Allen Browne said:
1. From the database window, open your subform in design view.

2. If you do not see a Form Footer section, click Form Header/Footer on
the
View menu. You can set the section's Visible property to No if you do not
wish to see this section.

3. In the Form Footer section, add a text box, and give it these
properties:
Control Source =Sum([Quantity] * [Price])
Name txtSubTotal
Format Currency

4. Save and close.

5. In the main form, set the Control Source of the text box that should
show
this total to:
=[Order Details Subform].[Form].[txtSubTotal]

You can then multiply that value by your Discount Multiplier field to
display the calculated total in another text box. (Note that you should
not
store the amount of discount nor the order total in the main Order
table.)

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Richard Horne said:
Hi guys.

I have a field in a sub form called Total Item Cost which multiplies
the
quantity and price of an item in my ordering system. This form is
called
Order Details Subform, then I have my main order form called Orders I
want
to
create a text box in this main form which SUMs the field Total Item
Cost,
can
this be done and if so how do I go about this?

Can you apply calculations from fields in a sub form to fields in a
main
form? As I also have a discount multiplier in the mai form I would like
to
multiply the SUM total by also. But obviously the answer to the first
part
of
my question should answer the second part.
 
Back
Top