473,748 Members | 10,889 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

populating a field using the sum of a field from a subform

43 New Member
Hi - I have a form called mainForm that has a subform called subForm. The subform has a numeric field called subCost. There are many subForm records for every mainForm record. On the main form, there is a field called mainCost.

Whenever someone makes a change to the subCost field, I want to populate the mainCost field with the sum of the subCost values. The mainForm and the subForm are joined by mainID = subID.

Here is my lame attempt at writing this myself (on the After Update event of the subCost field).
Expand|Select|Wrap|Line Numbers
  1. mainCost = "(SELECT Sum([subCost]) From subForm WHERE [subForm.subID] = [mainForm.mainID)"
Any help you can provide would be greatly appreciated.

Thanks in advance,
Bill
Aug 31 '09 #1
24 6485
ajalwaysus
266 Recognized Expert Contributor
You are on the right track, try just setting the control source of the MainCost in design view.

Expand|Select|Wrap|Line Numbers
  1. =SUM(forms!SubForm!SubCost)
Then try just re-querying the MainCost after update.

Expand|Select|Wrap|Line Numbers
  1. Private Sub subCost_AfterUpdate
  2.       me.parent.maincost.requery
  3. End Sub
  4.  
Aug 31 '09 #2
bkberg05
43 New Member
Hi and thanks. I don't think I can do it this way since the user needs to still be able to edit the mainCost field. Sometimes there won't even be a record on the subForm and the mainCost will have to be updated. Also it's record source needs to remain tied to an underlying table as I want to store data there.
Aug 31 '09 #3
ajalwaysus
266 Recognized Expert Contributor
You can change the control source on a need by need basis in the VBA code.

-AJ
Sep 1 '09 #4
bkberg05
43 New Member
Hi - I don't know how to do that. Is there a way I can set it to that value on the After Update of the subCost field. I tried:

Me.Parent.mainC ost = sum(forms!subFo rm!subCost)

But it doesn't seem to like the sum command.

I did try the =Sum(Forms!subF orm!subCost) as the control source for the mainCost field to see if I could make it work and it just displayed an error in the field.
Sep 1 '09 #5
ajalwaysus
266 Recognized Expert Contributor
Try this..

Expand|Select|Wrap|Line Numbers
  1. Private Sub SubCost_AfterUpdate
  2.     Me.Parent.mainCost.ControlSource = "=sum([forms]![subForm]![subCost])"
  3. End Sub
  4.  
When assigning a control source, you pass it the exact value you want, as you want it to appear in the properties. That is why we pass the full text above.

Let me know if this works.
-AJ
Sep 1 '09 #6
bkberg05
43 New Member
The field displays an #Error. Same thing that happened when I put that exact language into the control source. It strips out the brackets so all that's left is the =sum(forms!subF orm!subCost!). I tried putting in manually with and without the brackets. I also used the expression builder to get the syntax right and it was the same thing. It gave me =Sum([subForm].Form![subCost]). Same #Error that way.

I'm sure there's more than way to do this and even if I can get the Control Source to make the calculation, I still have to deal with storing the value of this field into the mainCost field and allowing the user to be able to change it.

Why can't we (on the After Update action of the subCost field) execute a query which makes the calculation and then stores the value into the mainCost field. Then I don't have to deal with storing the value from an unbound field and dealing with the user updating the field.

Thanks again for your help.
Sep 1 '09 #7
ajalwaysus
266 Recognized Expert Contributor
You said you wanted them to be able to update that field when there were no subCost(s). Perhaps, if you could, attach the DB in a zip file - stripped of all sensitive data - so that we can get to the heart of this issue, because I cannot understand why this did not work.

-AJ
Sep 1 '09 #8
bkberg05
43 New Member
Thanks and I've done that. We'll have to switch to the real field and form names.

Open the 'Order' form. The Subtotal field is the one I'm trying to update. It currently has the =sum(...) as the Control Source. It should be populating the field called Order_Cost_Subt otal in the Order table. The subform is called sub_Order_Items . The data behind the form is tied to the Order_Items table. I'm trying to total the amounts in the Order_Item_Exte nded_Cost field.

I've been putting the code for this (many variations as I've tried different things) on the AfterUpdate action for the Order_Item_Cost field on the subform. This same AfterUpdate action also updates the Order_Item_Exte nded_Cost field as this field is currently not enabled (it being calculated).

There is one record in here that I've been testing with. I change the Order_Item_Cost field and see if it works.

Thanks again for looking at this.
Attached Files
File Type: zip database copy.zip (377.7 KB, 169 views)
Sep 1 '09 #9
ajalwaysus
266 Recognized Expert Contributor
Sorry for the delay, I don't know why but it seems you cannot create a sum function on a main form that feeds from the subform. So I cheated a little and created a sum field on the subform and then related the subtotal field on the main form to pull the value of the sum on the subform. Hope I didn't lose you, in case i did I reattached your DB with my enhancement. You could also hide the field on your subform, and the reference will still work.

Let me know if you need anymore help,
-AJ
Attached Files
File Type: zip database copy.zip (430.2 KB, 184 views)
Sep 8 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
12594
by: sao | last post by:
I am currently using Access 2000. In my table it is the following fields that are set up: 1 - Staff Name 2 - Department This table is already populated with 100+ records (staff along with department.)
0
3637
by: Lauren Quantrell | last post by:
I'm using SQL Server backend on an Access 2K front end. I populate a subform: Forms!myForm.myChild.Form.RecordSource = "myStoredProcedureName" On that form is a control where the controlsource is "= Count()" that shows the user the number of records. What I'm doing now is populating the subform, using the RecordsetClone.RecordCount of the subform to determine if any records were returned, and if not, opening a message box telling the...
2
5787
by: Michelle | last post by:
Hi all I have used knowledge base article 112747 to improve my subforms performance. I am using Access 97. I have a main form where i put an extra textbox which concatenated TeamID WeekNum Weekday, ie value might be 15Monday. I then put in a field in my query behind my subform which held the same info and used these fields to link the mainform to the subform.
25
10257
by: Lyn | last post by:
Hi, I am working on a genealogy form. The only table (so far) lists everybody in the family, one record per person. Each record has an autonum ID. The parent form (frmMainForm) displays the data in each record, which includes the ID of the father and the mother (who also have records in the table). One record per form. I have a Tab Control in the form, and in one of the tabs I have a subform (sfmSiblings) in which I wish to list...
1
2970
by: tconway | last post by:
I have an Access program that displays Customer data into a form and OrderID data into a subform. The totals in the Subform are based on calculated fields, i.e. the Total Amount field Calculates the following: =* The Subform Order fields are pulled from a Query along with each calculated Total. They are displyed in a DataSheet View. This works fine on my developement environment which includes a server set up just as our client has. ...
9
9696
by: Ecohouse | last post by:
I have a main form with two subforms. The first subform has the child link to the main form identity key. subform1 - Master Field: SK Child Field: TrainingMasterSK The second subform has a master-child link to the first subform. subform2 - Master Field: subTrainingModule.Form!TrainingModuleTopicSK Child Field: TrainingModuleTopicSK
4
2661
by: Don Do | last post by:
Help I built a form/subform/subsubform setup using the access forms wizard. I have a table1 = parent, table2 = child, table3 = (grandchild?). There will be multiple records in table2 that tie to table1, and multiple records in table 3 that tie to table2. Both on "1 to many" joins. Each of the forms are bound to the fields in the respective tables. The subform & subsubform are datasheet view. When I enter any data, I
13
3548
by: Mary | last post by:
I'll pulling my hair out on this one and would be so appreciative of any help. I am creating a data entry form to enter results of a student survey. There are 40 questions on the survey. The first 7 have to do with respondent information like grade, class and age. I have those in a table with a PK of RespondentId, which is autonumbered. Questions 8-40 all have the same format. For these questions I have a table set up with the...
3
2022
by: GODSPEEDELECTRONICS | last post by:
My database is simple. It has a table that tracks customers, with they're name, address, etc. I have a price list table that contains "iteminstall" , "price", and "qty" and I have a payment table that trackes date, and payment amount. I have a subform, with the field (textbox) "price." I have it auto populate when I type in something under (combobox) "iteminstall". This is a recent addition. I used to have to type it manually. In...
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9541
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9247
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8242
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3312
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.