Solved

Main form text box reflects update of subform record

Posted on 2011-02-23
8
426 Views
Last Modified: 2012-05-11
I have a parent form with a total weight text box and a subform. Each subform record contains a weight. After each subform record is created or changed the total weight textbox on the parent form should reflect the total. I can use a select statement to query the uderlying subform table to calculate the total. My question is where do I put the select statement or what code do I need to add so that the parent form textbox reflects the total weight after a subform record is added or changed? Thanks.
0
Comment
Question by:StampIT
  • 4
  • 4
8 Comments
 
LVL 9

Expert Comment

by:borki
ID: 34965336
There is an easier solution.

In your subform add a total text box, that autosums the weights in the form. You can then reference that total text box in your main form.

See:
MS Access documentation onthe web
0
 

Author Comment

by:StampIT
ID: 34965390
borki,
    Do I put a text box in the subform footer that totals the weight? Do I reference the subform textbox with Forms!SubformName!txtBoxName in the main form? thanks
0
 
LVL 9

Accepted Solution

by:
borki earned 250 total points
ID: 34965520
Yes and yes, the text box goes in the subform footer and the text box in the main form has a data source of '=Forms!SubformName!txtBoxName'

Another example is at
http://www.databasedev.co.uk/subform-total.html

Good luck!
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!

 

Author Comment

by:StampIT
ID: 34969957
borki,
    This worked as you said. However I neglected to say that the tot weight field that is updated in the main form is a field in a table. The form serves as data entry to a table. I can update a textbox fine. How do I update the total weight control in the main form that is a field in the table? I don't know how clear this is. Don't know how else to explain it. Hope you can help. Thanks.
0
 
LVL 9

Expert Comment

by:borki
ID: 34973359
OK, you are saying that the total eight is not only just shown but is also a table field in the table that is bound to the main form. Generally, it is not recommended to have calculated fields in a table.

Once again there is an elegant solution: The filed showing the total from the subform needs to be unbound, it can not have the total weight as a control source. You the add another text box with the total weight as the control source. You set this text box's visible attribute to 'No'. You then add an event procedure to the 'Before Update' (and maybe 'Before Insert', if you are adding records to the main table) event. The code would simply be

Me.HiddenBoundTotalWeightField = Me.DisplayedCalculatedTotalWeightFromSubform

with the appropriate control names used.

HTH
0
 

Author Comment

by:StampIT
ID: 34988239
borki,
   I now have a bound text box called TotWeight and a unbound textbox called txtTotWtDisplay in the Main Form. txtTotWtDisplay contains the total weight from the subform and this appears to work fine. In the BeforeUpdate event of the Main Form I have Me.TotWeight = Me.txtTotWtDisplay. Unfortunately the TotWeight field remains empty even after I have closed out of the form. I also tried this in the BeforeInsert event with the same result. Am I missing something? Thanks.
0
 
LVL 9

Expert Comment

by:borki
ID: 34995845
I think you are starting to stretch the scope of the original question...

As I said before, calculated field values are always a bad idea, because the minute someone edits the related table's value (weight in your case) the data in your main table is invalid.

To get back to your immediate problem, I suspect that no data is changed in your main form, therefore the OnUpdate event probably does not fire...

Your best option is to initialise the weight when you first open a record (hence overwriting the existing value) using another bit of code:

Private Sub Form_Current()
   Me.TotalWeight = DSum("Weight", "tblSub", "MainID = " & Me.ID)
End Sub

HTH
0
 

Author Closing Comment

by:StampIT
ID: 34996977
Thanks for the help. I see what you mean about calculated fields in a table.
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

713 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