Results 1 to 6 of 6
  1. #1
    Clumsy is offline Novice
    Windows 10 Office 365
    Join Date
    Nov 2021
    Posts
    2

    Update Entry with SUM from other table

    Hello community,



    I am trying to update the entry with the ID "134" of the table "tbl_offers" with the sum of its Products (found in table "tbl_products").
    That's what i got so far, but it does not work...

    Code:
    UPDATE tbl_offers SET tbl_offers.total = (SELECT SUM(tbl_products.price) FROM tbl_products WHERE tbl_products.IDoffer = 134) WHERE tbl_offers.ID = 134;
    tbl_offers:
    ID total
    134 ??? (should be 15)
    ...

    tbl_products:
    ID IDoffer price
    1 134 10
    2 134 5
    3 999 80
    ...

    Would be thankful for any suggestions/help.
    Greetings from Bavaria, Germany
    Flo

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    Basically you do not, you just calculate when needed.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    Clumsy is offline Novice
    Windows 10 Office 365
    Join Date
    Nov 2021
    Posts
    2
    Hey, could you please explain in a bit more details?

    You suggest, that i do not save this information at all? -> Than my next Question would be, how to get all offers (sorted by total)?
    And even though it might not be necessary, I'd still like to know what i did wrong, so I can improve myself.

    Thanks for your reply!

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    Generally it is not recommended to store anything that you can calculate, as you are doing now.
    One might store a tax rate for a particularly period, rather than looking it up, like on order details, but in your case here, you would just use that inner query.
    As soon as you add a new record to tblOffers, that is out of date.? Bit like storing someone's age, going to be out of date within the year?, store the DOB instead, and calculate when needed.

    For your particular request, you would sum and group by IDOffer

    TBH, that SQL looks fine to me, but without data, I cannot test?
    I would break it down into small steps. Does that inner SQL work?, it should, then hardcode that number into the Update, does that work?, then put it all together.

    Edit: Always my first point of call
    https://www.google.com/search?q=upda...client=gws-wiz
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    CarlettoFed is offline Competent Performer
    Windows 7 64bit Access 2013 32bit
    Join Date
    Dec 2019
    Posts
    255
    You have to replace
    Code:
    UPDATE tbl_offers SET tbl_offers.total = (SELECT SUM(tbl_products.price) FROM tbl_products WHERE tbl_products.IDoffer = 134) WHERE tbl_offers.ID = 134;
    with
    Code:
    UPDATE tbl_offers SET tbl_offers.total = DSum("price","tbl_products","IDoffer = 134") WHERE tbl_offers.ID=134

  6. #6
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    As Gasman has suggested simply use a query to get your results

    SELECT IDOffer , Sum(price) as TotalOffers
    FROM
    tbl_products
    GROUP BY
    IDOffer

    It will always be accurate and doesn't need you to monitor for changes to the underlying table.

    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Update a Table depending on Form Entry
    By carterlangley in forum Programming
    Replies: 4
    Last Post: 03-10-2018, 01:39 AM
  2. Replies: 2
    Last Post: 10-14-2016, 05:05 AM
  3. Replies: 8
    Last Post: 05-30-2013, 05:06 PM
  4. Replies: 3
    Last Post: 02-07-2012, 06:33 AM
  5. Replies: 3
    Last Post: 05-19-2010, 10:08 PM

Tags for this Thread

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