简体   繁体   中英

How to Update a numeric field with the SUM of the numeric field in a different table MS-ACCESS 13

I have a supply inventory database with 3 tables. Main (Warehouse Stock), Supply Issue Table, and Supply Purchases Table. All three tables share the relationship ITEM #. Supply Issue and Supply Purchases both log transactions (ie Item #, Quantity Issued or purchased). I need to have the Purchase and Issue tables update the total quantity in the Main Table

I created an update query to update the two fields in the main table(PQuantity/IQuantity). Then a third auto-calculated field (MQuantity) which is (PQuantity-IQuantity). I did it this way because I understood I couldn't run an update query for an auto-calculated field?

Here's the update query SQL for purchases:

UPDATE [MAIN (WAREHOUSE STOCK)] 
INNER JOIN [Supply Purchases Table] 
ON [MAIN (WAREHOUSE STOCK)].[ITEM #] = [Supply Purchases Table].[ITEM #] 
SET [MAIN (WAREHOUSE STOCK)].PQUANTITY = +[Supply Purchases Table]![QTY  ADDED];

Here's the update query SQL for issues:

UPDATE [MAIN (WAREHOUSE STOCK)] 
INNER JOIN [Supply Issue Table] 
ON [MAIN (WAREHOUSE STOCK)].[ITEM #] = [Supply Issue Table].[ITEM #] 
SET [MAIN (WAREHOUSE STOCK)].IQUANTITY = +[Supply Issue Table]![QTY ISSUED];

The problem with this is it will only total the last purchase or issue that was input. I also get "Type conversion" errors.

Am I making this too difficult?

Thanks!

You have to add QTY ADDED to the old value of PQUANTITY like:

UPDATE
    [MAIN (WAREHOUSE STOCK)]
    INNER JOIN
        [Supply Purchases TABLE]
    ON
        [MAIN (WAREHOUSE STOCK)].[ITEM #]  = [Supply Purchases TABLE].[ITEM #]
    SET [MAIN (WAREHOUSE STOCK)].PQUANTITY = [MAIN (WAREHOUSE STOCK)].PQUANTITY + [Supply Purchases TABLE]![QTY ADDED];

But you should rethink you data structure, as usuallay you have only one table for transactions (IN/OUT) and compute the actual stock when needed. See Ms Featured Access Templates Inventory and Allen Browne AppInventory for some hints.

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