Results 1 to 5 of 5
  1. #1
    Cazca is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2015
    Posts
    28

    Field Calculating based off sum of other fields

    This is likely very easy for you experts to solve.

    I have two tables.

    Table1 has purchase agreements with a limit threshold on money to be billed against it. The agreements are defined by a two label ID (agreement & subagreement; see below example). Table2 list each invoice that places a purchase against that purchase agreement.

    How do I get a field in Table1 to add all the fields in Table2 that correspond to a record in Table1 and then subtract from a value in Table1? See below for example:

    Table1
    AutoNum AGREEMENT SubAgreement DollarValue DollarValueLeft
    1 PA1 sa1 $10
    2 PA1 sa2 $15
    3 PA2 sa1 $20 X
    4 PA2 sa2 $25

    Table2
    AutoNum InvoiceNum AGREEMENT SubAgreement AmountInvoiced
    1 1234 PA2 sa1 $7
    2 5678 PA2 sa1 $12

    For an example, where X is, I need 'DollarValueLeft' to equal the sum of all 'AmountInvoiced' values in Table2, that equal PA2 and sa1 and subtract that from 'DollarValue' on Table1.



    So for PA2-sa1, right now X should equal $1.

    Thoughts?

  2. #2
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    something like

    Code:
    SELECT T1.*, T1.DollarValue-T2.sumAmount AS DollarValueLeft
    FROM Table1 AS T1 INNER JOIN 
          (SELECT Agreement, SubAgreement, Sum(AmountInvoiced) AS sumAmount FROM Table2 GROUP BY Agreement, SubAgreement) AS T2 
              ON T1.Agreement=T2.Agreement AND T1.SubAgreement=T2.SubAgreement

  3. #3
    Cazca is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2015
    Posts
    28
    I'll give that a shot today, thanks man!

  4. #4
    Cazca is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2015
    Posts
    28
    Where would I put the code?

    Not in the expression builder for a calculated field, correct?

  5. #5
    bcmarshall is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Jul 2010
    Posts
    95
    Paste it into the SQL view of a Query design.

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

Similar Threads

  1. Replies: 4
    Last Post: 01-23-2015, 10:40 AM
  2. Replies: 3
    Last Post: 09-19-2014, 08:22 AM
  3. Replies: 1
    Last Post: 09-27-2013, 09:44 AM
  4. Replies: 0
    Last Post: 03-09-2012, 07:04 AM
  5. Replies: 3
    Last Post: 05-11-2011, 02:32 PM

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