Solved Append/Add Query Question (1 Viewer)

NearImpossible

Registered User.
Local time
Today, 09:21
Joined
Jul 12, 2019
Messages
225
Hi all, been awhile since i've been around, but working on another Access database and have a question so I figured what better place since you guys have helped in the past!!

I have 2 tables with one being a temp build table where I can do quantity calculations (Table1) before moving it to the final table (Table2)

My question is, Is it possible to append the data from table 1 to table 2, but if the item already exists in table 2, then the quantity would be added to the exisiting value vs appending it?

Example :

Temp Table1
ItemIDItemNameItemQuantityItemMeasurement
1Italian Seasoning2tbsp
2Salami3oz

Exisiting Table2
ItemIDItemNameItemQuantityItemMeasurement
1Italian Seasoning5tbsp
2Provolone Cheese3oz

End Result of Table2
ItemIDItemNameItemQuantityItemMeasurement
1Italian Seasoning7tbsp
2Provolone Cheese3oz
3Salami3oz

Please advise

thank you
 

isladogs

MVP / VIP
Local time
Today, 15:21
Joined
Jan 14, 2017
Messages
18,227
Yes. Use an UPSERT query AKA UPEND query.
Basically its an UPDATE query with an OUTER join so that it APPENDS unmatched records.

 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:21
Joined
Oct 29, 2018
Messages
21,473
Hi. Maybe this will help.
Edit: Oops, too slow...
 

NearImpossible

Registered User.
Local time
Today, 09:21
Joined
Jul 12, 2019
Messages
225
not sure what im missing, followed isladog's steps, but it doesn't appear to be doing anything even though it stats its updating records.

Trying to update the list table from the list build table.

thank you

Updated database file
 

Attachments

  • New Microsoft Access Database.accdb
    604 KB · Views: 85
Last edited:

XPS35

Active member
Local time
Today, 16:21
Joined
Jul 19, 2022
Messages
159
Create a separate update and append query.
It is important that you always perform them one after the other in the correct order. I assume that table1 can and should be emptied afterwards.
You should therefore execute the following queries in one and the same procedure.

Code:
UPDATE Table1 INNER JOIN Table2 ON Table1.ItemName = Table2.ItemName
SET Table2.ItemQuantity = [Table1].[ItemQuantity]+[Table2].[ItemQuantity];

INSERT INTO Table2
SELECT Table1.*
FROM Table1 LEFT JOIN Table2 ON Table1.ItemName = Table2.ItemName
WHERE Table2.ItemID Is Null;

DELETE Table1.* FROM Table1;
 

NearImpossible

Registered User.
Local time
Today, 09:21
Joined
Jul 12, 2019
Messages
225
Thanks guys, i'm going to start this DB over as I tried to take some shortcuts which are starting to bite me in the but, will be back if I need more assistance.

thanks again !!
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 10:21
Joined
May 21, 2018
Messages
8,529
Your sql was backward. You want to update the ShoppingList not the ShoppingListBuild
Code:
UPDATE
   ShoppingListBuild
   LEFT JOIN
      ShoppingList
      ON ShoppingListBuild.Ingredient = ShoppingList.Ingredient
SET
   ShoppingListBuild.[Meal Name] = [ShoppingListBuild] ! [Meal Name], ShoppingListBuild.Measurement = [ShoppingListBuild] ! [Measurement], ShoppingListBuild.Receipe_Size = [ShoppingListBuild] ! [Receipe_Size]
WHERE
   ShoppingListBuild.Ingredient) = [ShoppingList] ! [Ingredient]
  
;

You should have had
Code:
UPDATE
   ShoppingListBuild
   LEFT JOIN
      ShoppingList
      ON ShoppingListBuild.Ingredient = ShoppingList.Ingredient
SET
   ShoppingList.[Meal Name] = [ShoppingListBuild] ! [Meal Name], ShoppingList.Measurement = [ShoppingListBuild] ! [Measurement], ShoppingList.Receipe_Size = [ShoppingListBuild] ! [Receipe_Size];

Notice in the SET commands. You want to update the values in ShoppingList not in ShoppingListBuild. Also in you are doing a left outer join so there is no where condition.
 

isladogs

MVP / VIP
Local time
Today, 15:21
Joined
Jan 14, 2017
Messages
18,227
Agree with @MajP but a few other points:
1. Both tables should have a primary key field. In this case, ID would be appropriate as a PK field
2. Why duplicate the same data in 2 tables? Data should only be stored once
3. If you must do this then why not update all fields?
4. Use the ID field to join the 2 tables in your query.
5. Suggest your UPSERT/UPEND query becomes:

Code:
UPDATE ShoppingListBuild
    LEFT JOIN ShoppingList ON ShoppingListBuild.ItemID = ShoppingList.ItemID
SET ShoppingList.[Meal Name] = [ShoppingListBuild]![Meal Name], ShoppingList.Meal_Servings = [ShoppingListBuild]![Meal_Servings],
      ShoppingList.[Select] = [ShoppingListBuild].[Select], ShoppingList.Measurement = [ShoppingListBuild]![Measurement],
     ShoppingList.Receipe_Size = [ShoppingListBuild]![Receipe_Size], ShoppingList.Ingredient = [ShoppingListBuild].[Ingredient];
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 10:21
Joined
Feb 19, 2002
Messages
43,275
I think the OP wants to increment certain columns NOT replace them. I don't see any sample that does that.
 

Users who are viewing this thread

Top Bottom