Form only works if I manually step thru code Version: 2000

G

Guest

I have a form that tracks Invoices. It has a subform on it that sums all of
the invoice items up and then is supposed to add that sum to a control on the
main form that contains the base price. I can only get the form to do this
when I have a breakpoint in my code and I manually step thru it. If I just
open the form w/o the breakpoint, the calculation only gives me the base
price and the subform sum instead of them added together.

I'd appreciate any help I can get.
Thanks,
Tina
 
A

Allen Browne

Okay: if it works in break mode, it's a timing issue, i.e. Access has not
updated the calculated control by the time you are reading the value. As a
workaround, you could use DSum() to get the total from the table directly.

An underlying question is, Should you be storing the total in the main
form's table at all? In general, you don't want to do that, unless you have
a special reason such as calculating a freight or discount that depends on
the total being in a certain bracket.
 
G

Guest

I calculate the base price of the invoice on the main form. Then the subform
contains any extra add-on costs. I get the sum of the cost of add-ons and
bring that into a control on the main form. Then I try to add the base price
and the control that shows the sum of add-ons into another control on the
main form.

Where should I use the DSum? In the subform or on the main form?
 
A

Allen Browne

Do the DSum() in the AfterUpdate event procedure of the subform. That's the
event that runs whenever there is a change in the subform.

You need to do it in the subform's AfterDelConfirm event as well, to
recalculate the total when a subform record is deleted.

For an example of how to do this without storing the total at all (so it can
*never* be wrong), open the NorthWind sample database. Open the Orders form.
You will see how they just ask Access to give them the subtotal directly
from the subform, add on the items from the main form (freight in their
example), and show the calculated total. That's the way to go if you
possibly can.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top