473,387 Members | 3,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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 6441
ajalwaysus
266 Expert 100+
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
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 Expert 100+
You can change the control source on a need by need basis in the VBA code.

-AJ
Sep 1 '09 #4
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.mainCost = sum(forms!subForm!subCost)

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

I did try the =Sum(Forms!subForm!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 Expert 100+
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
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!subForm!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 Expert 100+
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
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_Subtotal 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_Extended_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_Extended_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 Expert 100+
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, 183 views)
Sep 8 '09 #10
Hi and thanks! It displays fine when you first go to a record, but when you're adding or updating a record, the sum fields don't also update. Do you know how to make them update. I tried doing a .refresh for the field but it didn't like that.

Sorry to drag this out, but appreciate your patience.

Thanks
Sep 8 '09 #11
ajalwaysus
266 Expert 100+
Try ".Requry" on both the sub-form and main-form field. Let me know if that works.

No apology needed, the goal is to get you a solution.

-AJ
Sep 9 '09 #12
No luck. The requery doesn't give an error, but nothing happens. I just did it to the field on the subform. Any other suggestions?
Sep 10 '09 #13
ajalwaysus
266 Expert 100+
Update your VBA Order_Item_Cost_AfterUpdate() code with this..

Expand|Select|Wrap|Line Numbers
  1. Private Sub Order_Item_Cost_AfterUpdate()
  2.  
  3.     Order_Item_Extended_Cost = Order_Item_Quantity * Order_Item_Cost
  4.     Me.Order_Cost_Subtotal.Requery
  5.     Me.Parent.Order_Cost_Subtotal.Requery
  6.  
  7. End Sub
  8.  
Let me know if this works, it worked for me, so if it doesn't you need to spell out exactly what you are doing.

-AJ
Sep 10 '09 #14
Hi - I wasn't using the me. in front of the field. So now that works. Now I need to get that information into a bound field on the main form which someone can then edit if they need to. I tried setting the value of the bound field as fieldname.value = unboundfieldname.value (tried it both for the unbound field on the main and the subform). I did this right after the requery command. The data in the bound field changed to zero for some reason (both when I used the value of the unbound field on the form and the subform).I also tried it on the after update and change action of both the unbound fields and that didn't work either.

Feels like we're getting close, but may need some more of your assistance...

Thanks
Sep 10 '09 #15
ajalwaysus
266 Expert 100+
OK, but first I have to ask:
Why are you allowing someone to edit a field that displays a subtotal, if that happens then the subtotal won't match what the actual total is in the subform?

-AJ
Sep 10 '09 #16
I use this to track orders. For most orders, it's not important to me or my business to track the details for those orders (the individual items) and sometimes the individual items may number into the hundreds and I don't have time to put them all in there. So usually I just put in the order and then start at the subtotal field and put the amount of the order prior to shipping and tax. But sometimes the detail is important to me and I do enter detail records. With the data in the detail records already, I figured why should I hand calculate the subtotal - I should just let the form do it for me. And that's where we're at.
Sep 10 '09 #17
ajalwaysus
266 Expert 100+
So if I understand you correctly, sometimes you just want to skip the order by order entry and just enter an amount owed without details.
If so, you may want to consider adding a field that actually stores this value that is equal to the subtotal but can be overwritten. This way you will see the subtotal, and then have an actual total owed field that is separate to do with what you please.

Make Sense?
-AJ
Sep 10 '09 #18
That's the goal. I need to store the sub-total either way. Sometimes it's the sum from the subform. Sometimes it's entered directly.
Sep 10 '09 #19
ajalwaysus
266 Expert 100+
But what I am saying is perhaps you will want to store both in any case, this way you have a record of what was calculated by the DB and you will also have what was manually entered by the person.

You can bound both fields to the table as separate fields in the table. (i.e. Calculated Total, Manual Total)

Then to set the value of the Calculated Total, instead of keeping the code I have in the subtotal field, assign the value on the VBA side, and lock this text box.
Then create the second text fields bound to Manual Total, and assign the subtotal to it as well, but leave it unlocked so that i can be changed if they wish.

Hope this makes sense, let me know if I am not.
-AJ
Sep 10 '09 #20
I'm good with this. I just can't figure out the part about assigning the value on the VBA side for the Calculated Total. I don't know how to make that happened and have tried a variety of things...
Sep 10 '09 #21
ajalwaysus
266 Expert 100+
you just need to write code probably on the Form_Activate(). You need to write some code along the lines of:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Activate()
  2.     Me.Order_Cost_Subtotal.Value = [enter the code to retrieve the value from the subform]
  3. End If
  4.  
I am here to help, but I also would like you to get the hang of this without me giving you the answer outright.

I can answer any questions you have to the best of my ability. Don't hesitate to ask.

-AJ
Sep 10 '09 #22
Why the Activate event? I've not used that, but it says this runs when the form opens and becomes the active form. Which means it won't run when I move from record to record.

Even if the activate worked when moving from the sub-form to the regular form, I don't want the value on the regular form to wait that long. I want to update it from while I'm still inside the subform.

Why can't this be done on the After Update event on the Order_Item_Cost field?

And I've tried (on the After Update event of the Order_Item_Cost field)

Me.Parent.Order_Cost_Subtotal.Value = sub_Order_Item_Extended_Subtotal (which is what I called the sum field in the footer of the subform)

I've also tried a bunch of other variations and the best I can get it to do is to change to zero, which is odd but seems like my syntax is correct.
Sep 10 '09 #23
ajalwaysus
266 Expert 100+
for future reference you need to use the [code] Tags when posting code, the button looks like a "#".

Now try this:
Expand|Select|Wrap|Line Numbers
  1. Me.Parent.Order_Cost_Subtotal.Value = me.sub_Order_Item_Extended_Subtotal.value
  2. Me.Parent.Order_Cost_Subtotal.Requery
  3.  
-AJ
Sep 10 '09 #24
Expand|Select|Wrap|Line Numbers
  1. Private Sub Order_Item_Cost_AfterUpdate()
  2.  
  3.     Order_Item_Extended_Cost = Order_Item_Quantity * Order_Item_Cost
  4.     Me.sum_on_sub_form.Requery
  5.     Me.Parent.sum_on_main_form.Requery
  6.  
  7. End Sub
  8.  
  9. Private Sub Order_Item_Cost_LostFocus()
  10.  
  11.      Me.Parent.Order_Cost_Subtotal.Value = Me.sum_on_sub_form.Value
  12.  
  13. End Sub
  14.  
This is as close as I've gotten it. I changed the sum field names so I could keep track of them better. The result here is that if you change the amount and hit tab, the cursor stays in the amount field and the two sum fields change (the one on the main form and the one on the subform). Then tab again and the subtotal field on the main form changes as I desired. I have no idea why the first tab doesn't leave the field.

Putting the
Expand|Select|Wrap|Line Numbers
  1.      Me.Parent.Order_Cost_Subtotal.Value = Me.sum_on_sub_form.Value
  2.  
in the After Update instead of the Lost Focus does nothing. It just zeros out the field on the main form.

Adding the requery you suggested in the last post did nothing at all regardless of where the
Expand|Select|Wrap|Line Numbers
  1.      Me.Parent.Order_Cost_Subtotal.Value = Me.sum_on_sub_form.Value
  2.  
was located.
Sep 10 '09 #25

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

Similar topics

3
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...
0
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...
2
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...
25
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...
1
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...
9
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...
4
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...
13
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...
3
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...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...

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.