Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    TopWebb is offline Novice
    Windows 8 Access 2013
    Join Date
    Feb 2014
    Posts
    4

    How to save a field calculated on a form to its table

    I have a form into which I enter items purchased, description, quantity, price, etc. This info is saved to a table. I would like to capture the total initial cost in the table as well so that even if the price or quantity subsequently change, I know the initial cost.

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    You can create a new field/column in your table and bind a new control to the new field.

    Me.NewControl.Value = Me.CalculatedControl.Value

  3. #3
    TopWebb is offline Novice
    Windows 8 Access 2013
    Join Date
    Feb 2014
    Posts
    4
    Thanks for the suggestion. I tried something like that before I made the post and just tried again, without success.
    I placed into the 'Control Source' the following: [widgets]![Total Cost]=[quantity]*[cost]. The formula works fine by itself ([quantity]*[cost]) but, of course, doesn't save anything to the table. Adding the [widgets]![Total Cost] before the calculation doesn't cause it to save anything.
    I suspect I need to put something into one of the 'On ...' fields, perhaps 'On Exit'. I haven't used these in the past and need some guidance.

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    One method:

    Add a hidden control to the form bound to the new field. Use the FORM BeforeUpdate event to put the calculated value into the new hidden control.

    "TotalCost" is the NEW hidden control (notice NO spaces)
    "TotalCalcCost" is the visible calulcated control (notice NO spaces)

    In the FORM BeforeUpdate event:
    Me.[widgets]![TotalCost] = Me.[widgets]![TotalCalcCost]

  5. #5
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    You can create a new event handler in the VBA module by going to the property sheet and selecting the Event tab. You will want to make sure that you are viewing the properties for the Form.
    .
    Click image for larger version. 

Name:	PropertyWindow.jpg 
Views:	28 
Size:	66.0 KB 
ID:	15365

    From there you will want to click the ellipses (...) next to the appropriate event. Then, select code builder. This will take you to the IDE and the display your new sub procedure. Place your code in the middle.

    Here is an illustration of focus on the RowSource property. You can see the ellipsis there.
    .
    Click image for larger version. 

Name:	QueryBuilderCombo.jpg 
Views:	28 
Size:	51.9 KB 
ID:	15366

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Don't think the hidden bound control is necessary. Can just populate the field.

    Me!TotalCost = Me.TotalCalcCost

    Actually, I recommend not saving the calculated total, instead save the raw data (quantity, price, tax rate, etc) and do calc when needed. Saving calculated data (data dependent on other data) is usually bad idea.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    TopWebb is offline Novice
    Windows 8 Access 2013
    Join Date
    Feb 2014
    Posts
    4
    Steve -
    Thank you for your suggestion. I added a control that I could eventually hide and bound it to the new, Total Cost field in the related table.
    Then I tried various different ways to calculate the value. I put it into the before and also the after field in the Properties for the Control. I also tried it in the Before and also the After field in the Properties for the Form. Nothing seemed to make a difference. I also tried '=[TotalCalcCost]'
    Some notes: Since this control on the form was referencing other controls on the same form, the [widgets]! did not show. Also, I entered "Me." at the beginning. Was that supposed to be a substitute for something else?
    I suspect this is something relatively simple that I have just not been able to find. I appreciate your assistance.
    btw, I really like your tag: Veni, Vidi, Velcro.
    -Webb

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Me is a VBA alias for the form or report object the code is behind.

    Did you consider my comments in prior post?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    MarvinM's Avatar
    MarvinM is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Dec 2013
    Posts
    30

    Thumbs up

    TopWebb,

    June is absolutely right about it being wise not to store calculated values in a table...
    unless you have a good reason for it. I saw in your original post that the factors (like price) could change, but you do not want the calculated value to change accordingly. I would call that a good reason.

    As for your question, your suspicion is correct. I would use the On Current event for the Form. Let's say the name of your textbox control where you calculate the total is named TotalCalcCost (Please don't use spaces.) And let's say that the field in your bound table that you want to populate is named TotInitCost. You are correct to put the source for your control =[quantity]*[cost]. Then for the On Current event for the form (click on the ellipses) add one line of code between the "Sub" and "End Sub" lines like this:
    Code:
    Private Sub Form_Current()
         TotInitCost = TotalCalcCost
    End Sub
    That should do it. Good Luck!

  10. #10
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    That's why I suggested storing the price (and other raw data that could change) in effect at the time of the order. It's called a paper trail and CYA. It documents what contributes to the calculated value.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    MarvinM's Avatar
    MarvinM is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Dec 2013
    Posts
    30
    Ah, Yes!
    That is a very good point. If there is a fixed price for the product, he could have a price history for each item. I think that's called Type 2 data. If the prices change from one order to another, it may be due to a discount, which would need to be tracked too. This sounds like it is a table of Items Purchased, rather than something which he would have any control over the price. I like your thinking. It is better to keep track of the details.

  12. #12
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    @June, I've always had to reference a control on a form to be able to store a calculated value. I tried using the bang instead of the dot, but the value wasn't stored. I had to have the control on the form.


    @TopWebb

    OK, to store the total initial cost you need a field in the form record source. So add a field to the appropriate table. I would name it something like "TotInitCost".
    Make sure the new field appears in the form record source. I say form record source because I base all of my forms on queries.

    In the query I have a column like "TotCostcalc: [quantity]*[cost]". I like doing calculations in queries, not in a form.
    Next, bind the control on the form named "[CostCalc]" to the field in the query named "TotCostCalc".

    Now we need to get the calculated value from the "[CostCalc]" control and put it in the field "TotInitCost".
    Add a form event. Set the form before update event to
    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
          Me.TotalCost = Me.costCalc
    End Sub
    If you ever change the quanity or code in this record, the initial cost will also change. You could prevent that by adding a test like:
    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
       If IsNull(Me.TotalCost) Then
          Me.TotalCost = Me.costCalc
       End If
    End Sub
    Once this is working, don't forget to hide the total initial cost control (set the visible property to false).


    This is the method I use. I also have to save calculated values due source values changing and I have to keep a history. I try not to save calculated values, but sometimes it is necessary.

    ---
    attached is an Access 2000 mdb....

  13. #13
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    @Steve, interesting - I have lots of code saving values to fields without a bound control. Would have been a real headache if I had to create a bunch of hidden textboxes.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  14. #14
    TopWebb is offline Novice
    Windows 8 Access 2013
    Join Date
    Feb 2014
    Posts
    4
    Thank you for this clarification although I'm not too sure how to use the info.
    I am a fairly competent user of Access in that I can design good systems to handle fairly complex requirements. However, I only use the tools I find in the Tables, Queries, Forms, Reports, and Macros. I'm aware that some more indepth programming is available, but I have never taken that task on.
    As far as the reason to save the calculated value, for this application, I wish to save the initial financial outlay. While quantity or cost may change over time and I will be tracking that, I also wish to keep a record of the initial cost. Perhaps the best way is to just enter it and not try to get fancy by making the calculation.

  15. #15
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    @June, I tried what you posted in Post #6. Maybe I entered something wrong. I'll try again.

    The field to update is in the form record source, not bound to a control.

    You use VBA like "Me!FieldName = Me.CalcAmount" where "Me!FieldName" refers to a field in the record source and "Me.CalcAmount" is the new calculated data in a control on a form??

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 04-21-2013, 03:20 PM
  2. Replies: 1
    Last Post: 01-09-2013, 02:42 PM
  3. Calculated field for table not form
    By cheyanne in forum Access
    Replies: 1
    Last Post: 01-27-2012, 07:29 AM
  4. Replies: 2
    Last Post: 01-09-2012, 08:15 AM
  5. Update table from calculated field in a form
    By BernardKane in forum Forms
    Replies: 3
    Last Post: 11-28-2006, 09:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums