Update Table Column with Sum of Two Numbers in Two Tables

Keith A 40 Reputation points
2025-11-07T16:18:05.6666667+00:00

Relationship

I have ran into a brick wall trying to create an update query. I want to update the number stored in the column of a table with the sum of another number in the same table and another number in another table. Attached is the relationship between the tables. The ServiceLog table has thousands of rows and the Services table has a few rows. What I want to do is create a button on a form that updates the LNextDue value in the ServiceLog table with the sum of LMileage and SMiles where the the record's LServiceID equals the ID of Services. I've tried a dozen different ways with no results or with the error "Operation must use an updateable query". Any help would be greatly appreciated.

Microsoft 365 and Office | Access | Development
0 comments No comments
{count} votes

Answer accepted by question author
  1. Ken Sheridan 3,546 Reputation points
    2025-11-08T12:44:15.09+00:00

    I think the problem you have encountered is due to your confusing summation and addition.  In this case it is the latter you want. 

    Normally you would not store the mileage when a service is next due.  It can be computed easily in a query.  To store the value in a column in the ServiceLog table introduces redundancy, and the consequent risk of update anomalies.  Having dropped the LNextDue column from the table, the query would then be:

    SELECT
        ServiceLog.*,
        LMileage + SMiles AS LNextDue
    FROM
        ServiceLog INNER JOIN Services
        ON ServiceLog.LServiceID = Services.ID
    ORDER BY
        LDate;
    

     In your case you would only store the mileage when a service is next due if you wish to maintain the values in previous rows on the basis of the original service interval, while computing subsequent values on the basis of the amended service interval.  In that context Viorel has given you the correct solution.

     I notice that the relationship between the tables is not enforced.  An unenforced relationship serves no useful purpose.  To enforce the relationship select the Enforce Referential Integrity option in the Edit Relationships dialogue. 

     

    2 people found this answer helpful.

Answer accepted by question author
  1. Viorel 125.6K Reputation points
    2025-11-07T20:41:39.4233333+00:00

    Try this query too:

    UPDATE ServiceLog
    INNER JOIN Services ON ServiceLog.LServiceID = Services.ID
    SET
        ServiceLog.LNextDue = [ServiceLog].[LMileage] + [Services].[SMiles];
    
    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Q&A Assist
    2025-11-07T16:18:27.0633333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To create an update query that updates the LNextDue value in the ServiceLog table with the sum of LMileage from the ServiceLog table and SMiles from the Services table based on the matching LServiceID, you can use the following SQL syntax:

    UPDATE ServiceLog
    SET LNextDue = LMileage + (SELECT SMiles FROM Services WHERE Services.ID = ServiceLog.LServiceID)
    WHERE LServiceID IN (SELECT ID FROM Services);
    

    Explanation:

    • The UPDATE statement targets the ServiceLog table.
    • The SET clause specifies that LNextDue should be updated to the sum of LMileage and SMiles from the Services table.
    • The subquery (SELECT SMiles FROM Services WHERE Services.ID = ServiceLog.LServiceID) retrieves the SMiles value corresponding to the LServiceID in the ServiceLog table.
    • The WHERE clause ensures that only records in ServiceLog that have a corresponding ID in the Services table are updated.

    Make sure to test this query in a safe environment before applying it to your production database, as updates cannot be undone without a backup.

    0 comments No comments

  2. Duane Hookom 26,545 Reputation points Volunteer Moderator
    2025-11-07T17:53:45.7666667+00:00

    Typically, you don't want to store values that can be computed/calculated on-the-fly. Your question didn't mention either the date or vehicle fields in your log table. I expect one or both of these are significant.

    I anticipate the use of DSum() could solve your problem since any aggregate query will render your table read-only.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.