Results 1 to 4 of 4
  1. #1
    victor is offline Novice
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    3

    calculate value from two tables

    Attached is a database with two tables. i would like to calculate the difference between field amount(table invoice) and amount paid(payments). the problem is when i linked the two tables using patient ID it replicates the amount paid more then the number. say receipt 24 will appear for each column of patient id in the invoice table

  2. #2
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,044
    Hi,

    you need to include Invoice.[issue ID] to the table Payments to be able to see which payment is for which invoice. In fact you probably need at minimum the following tables:
    *tblPatients
    *tblInvoices
    *eventually tblInvoiceLines if one invoice can have more then one productline
    *eventually tblProducts(drug, consultation, ...)
    *tblPayments: this should include which patient pays for which invoice

    For the moment all you can do is make two totalsqueries: one for the amount due per patient and one per amount payed per patient. Then you link those two queries by the patient ID. Then you'll be able to see if the totals match.

    succes
    NG

    PS: it's bad practice to use spaces in object names, this only leads to trouble later on.

  3. #3
    NTC is offline VIP
    Windows Vista Access 2007
    Join Date
    Nov 2009
    Posts
    2,392
    Sorry I don't look at attachments; in general - if one wants to solve this at the query level; when one table has multiple records - make a query that results in one record...then make a second query that links the first query with the other table. That first query may be an aggregate query or distinct query depending on the correct solution.

    One can also approach this from at the form/report level using main/sub forms/reports and do comparative math between appropriate fields. This approach may or may not be right depending on the situation.

    Hope this helps a little.

  4. #4
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by victor View Post
    Attached is a database with two tables. i would like to calculate the difference between field amount(table invoice) and amount paid(payments). the problem is when i linked the two tables using patient ID it replicates the amount paid more then the number. say receipt 24 will appear for each column of patient id in the invoice table
    VICTOR,

    your data is very much disoriented. it needs to be normalized. try this to fix your current problem though:
    Code:
    SELECT DISTINCT 
    
    Invoice.[Patient ID], 
    
    NZ(DSUM("[QUANTITY] * [PRICE]", "INVOICE", 
    
    "[PATIENT ID] = " & [PATIENT ID]), 0) AS TotalPurchased, 
    
    NZ(DSUM("[AMOUNT PAID]", "PAYMENTS", 
    
    "[PATIENT ID] = " & [PATIENT ID]), 0) AS TotalPaid, 
    
    [TotalPurchased] - [TotalPaid] AS CurrentBalance
    
    FROM Invoice;

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

Similar Threads

  1. Calculate Interest
    By jgelpi16 in forum Queries
    Replies: 3
    Last Post: 08-19-2016, 08:01 PM
  2. Replies: 5
    Last Post: 05-05-2010, 04:31 PM
  3. Calculate avg in a report
    By bob006 in forum Reports
    Replies: 1
    Last Post: 11-18-2009, 06:31 PM
  4. Replies: 0
    Last Post: 02-26-2009, 04:30 PM
  5. calculate holidays
    By barkarlo in forum Queries
    Replies: 0
    Last Post: 12-20-2006, 06:08 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