Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 176
  • Last Modified:

Help with total from subform to form and report

I have a Invoice database that I need put a Sum of Price from a subform and put on form and also on a report for printing the same thing on form. Here is what I started with Invoiceold and Invoice is the new one. Please look at what I am doing so you understand my thinking.
Invoice.zip
0
Soupbone79
Asked:
Soupbone79
  • 11
  • 7
  • 4
2 Solutions
 
FlysterCommented:
Try using this DSum formula in the total text box (Text6) on your Invoice Form:

=DSum("Price","Euipment Table","InvoiceID= " & [InvoiceID])

Flyster
0
 
FlysterCommented:
In your report, add a text box and use this formula as the control source:

=Format(DSum("Price","Euipment Table","InvoiceID= " & [InvoiceID]),"$0,000.00")
0
 
Soupbone79Author Commented:
that works. but it gives error on new record until put something in. Then it doesn't show total until you close form and reopen it.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Gustav BrockCIOCommented:
Call a simple function like this from the AfterUpdate and AfterDelConfirm events of the subform:
Private Sub CalculateTotal()

    Dim rst As DAO.Recordset
    Dim LineSum As Currency

    LineSum = 0
    Set rst = Me.RecordsetClone
    If rst.RecordCount > 0 Then
        rst.MoveFirst
        Do Until rst.EOF
            LineSum = LineSum + Nz(rst!LineTotal.Value, 0)
            rst.MoveNext
        Loop
    End If
    rst.Close

    Me.Parent!LineSum.Value = LineSum

    Set rst = Nothing

End Sub

Open in new window

Using Sum (in the form as controlsource of a textbox) or DSum is too slow in most cases.

/gustav
0
 
Soupbone79Author Commented:
Ok the code for auto update did not work did I do something wrong? I did change the Dsum To Sum. Here is my changes in file
Invoicenew-.zip
0
 
Gustav BrockCIOCommented:
DSum will be too slow. It's much faster to recalculate from within the subform than waiting for the main form to discover that something has changed.

/gustav
0
 
Soupbone79Author Commented:
Sorry I have got it work right in subform show in the main form yet. I got it work in report no problem when I print. when I go to add new record it shows #error until some data is imputed the it is blank until I close form then reopen it. If I click my command button to print (lunching Invoice report) it shows total. But when I come back to form no total until I close form and reopen it.
0
 
FlysterCommented:
Gustav is correct about the speed of DSum. Try this expression in you Invoice Form total text box (Text6)

=[Euipment Table subform].[Form]![Text8]
0
 
Gustav BrockCIOCommented:
If you use DSum, you will have to requery the field on the main form for any change of the amount in the subform. Sum may work but with a delay. Been there, done that. It is just to slow for busy users.

/gustav
0
 
FlysterCommented:
Here is a sample using a non VBA approach. For the Invoice Form, the totals are obtained from the text box you placed in the subform. For the report, another price textbox was added and set to running total over group (Visible=False)
Invoice-.mdb
0
 
Soupbone79Author Commented:
I look at yours no sure where you are going with that, sorry. Look at mine now (in the form and other places) and see what it does when you add new record and before and after input of data and also click print command button. Go ahead and try all command buttons.
Invoice--new2.zip
0
 
Gustav BrockCIOCommented:
Your form has been modified to work.

/gustav
Invoice-works-.zip
0
 
Soupbone79Author Commented:
sorry forget that one try this one.
Invoice-new-3.zip
0
 
Soupbone79Author Commented:
Thank you gustav ! once again you blow me away. so the code was
Private Sub Form_Current()

    Call Me.Euipment_Table_subform.Form.CalculateTotal
   
End Sub

was that the fix on the form?
0
 
Gustav BrockCIOCommented:
I just copied and pasted the code i posted previously, and adjusted to the name of your amount-field (Price).

And then I added the call from the main form which I had forgot. It sets the value when switching invoice ID.

/gustav
0
 
Soupbone79Author Commented:
was that on the subform?
0
 
Soupbone79Author Commented:
how di you get it to go to the unbound?
0
 
Soupbone79Author Commented:
sorry bad typing how did you get it on the unbound txt box on the form?
0
 
Soupbone79Author Commented:
was this it on the subform

Private Sub Form_AfterDelConfirm(Status As Integer)

    Call CalculateTotal

End Sub

Private Sub Form_AfterUpdate()

    Call CalculateTotal

End Sub

Public Sub CalculateTotal()

    Dim rst As DAO.Recordset
    Dim LineSum As Currency

    LineSum = 0
    Set rst = Me.RecordsetClone
    If rst.RecordCount > 0 Then
        rst.MoveFirst
        Do Until rst.EOF
            LineSum = LineSum + Nz(rst!Price.Value, 0)
            rst.MoveNext
        Loop
    End If
    rst.Close

    Me.Parent!txtTotal.Value = LineSum

    Set rst = Nothing

End Sub
0
 
Gustav BrockCIOCommented:
0
 
Soupbone79Author Commented:
Well gustav thank you again! Maybe someday I will learn enough that wont have bother you all.
0
 
Gustav BrockCIOCommented:
You are welcome!

/gustav
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 11
  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now