简体   繁体   中英

MS Access: How would I update a value in a table to the sum of said value and the value from a query?

If I have the table tblValues with the following records

RecordMonth(PK)   |   TotalRecords   |   TotalVariances
Jan               |   15             |   2
Feb               |   10             |   1
Mar               |   8              |   0
.                 |                  |
.                 |                  |
.                 |                  |
Dec               |   11             |   1

and the query qryDailyValues with the following records calculated from a separate temporary table that is deleted and repopulated each day when we perform an item count

RecordMonth   |   CountOfTodaysRecords   |   CountOfTodaysVariances
Feb           |   2                      |   1

How would I go about updating the appropriate tblValues record (in this case Feb since that is when the count took place) with the sum of qryDailyValues.CountOfTodaysRecords and tblValues.TotalRecords ? End result being the Feb record's TotalRecords set to 12 (10+2) and TotalVariances set to 2 (1+1).

Every time I try and create an update query to update Field: TotalRecords from Table: tblValues and Update To: =TotalRecords + qryDailyValues.CountOfTodaysRecords , it just prompts me for the qryDailyValues.CountOfTodaysRecords parameter instead of just pulling it from the query. Same goes for updating TotalVariances with itself plus CountOfTodaysVariances .

After some more research, I realized I made two mistakes that prevented me my update query from working.

First, my select query ( qryDailyValues ) was not "updateable" due to it containing aggregate functions. I resolved this by turning it into a make table query, allowing it to create a temporary table, that is then deleted and remade every time it is ran.

Second, my update query needed to have the destination table joined to the new temporary source table (created by the make table query) on the related field, RecordMonth .

After all of this was setup properly, the update query properly pulled values from the temporary source table, and added them to the existing values of the destination table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM