Solved

Help with total from subform to form and report

Posted on 2014-11-30
22
160 Views
Last Modified: 2014-12-02
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
Comment
Question by:Soupbone79
  • 11
  • 7
  • 4
22 Comments
 
LVL 22

Expert Comment

by:Flyster
ID: 40472941
Try using this DSum formula in the total text box (Text6) on your Invoice Form:

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

Flyster
0
 
LVL 22

Expert Comment

by:Flyster
ID: 40472944
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
 

Author Comment

by:Soupbone79
ID: 40472974
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
 
LVL 49

Assisted Solution

by:Gustav Brock
Gustav Brock earned 400 total points
ID: 40473341
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
 

Author Comment

by:Soupbone79
ID: 40473541
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
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40473571
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
 

Author Comment

by:Soupbone79
ID: 40473758
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
 
LVL 22

Accepted Solution

by:
Flyster earned 100 total points
ID: 40473775
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
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40473800
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
 
LVL 22

Expert Comment

by:Flyster
ID: 40473906
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
 

Author Comment

by:Soupbone79
ID: 40474361
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40474409
Your form has been modified to work.

/gustav
Invoice-works-.zip
0
 

Author Comment

by:Soupbone79
ID: 40474410
sorry forget that one try this one.
Invoice-new-3.zip
0
 

Author Comment

by:Soupbone79
ID: 40474439
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
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40474453
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
 

Author Comment

by:Soupbone79
ID: 40474465
was that on the subform?
0
 

Author Comment

by:Soupbone79
ID: 40474469
how di you get it to go to the unbound?
0
 

Author Comment

by:Soupbone79
ID: 40474472
sorry bad typing how did you get it on the unbound txt box on the form?
0
 

Author Comment

by:Soupbone79
ID: 40474480
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
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40474807
0
 

Author Comment

by:Soupbone79
ID: 40474952
Well gustav thank you again! Maybe someday I will learn enough that wont have bother you all.
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40475582
You are welcome!

/gustav
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now