Solved

Main form text box reflects update of subform record

Posted on 2011-02-23
8
424 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…

786 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