Question about appending a query to a table

HOTWATT
HOTWATT used Ask the Experts™
on
I am appending a query to a table. The question I have is can you replace the old data in the table with the new data from the query without having to go into the table and delete the old data? Right now if you click append twice it will just keep adding the same information. I want it to overwrite the old info in the table.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Paul Cook-GilesSenior Application Developer
Commented:
If your table, and the source of the query, share a unique identifier (the primary key), then you can create an update query by matching your table to the source and updating the non-key fields.  Otherwise, run two queries:  a delete query to empty the table, and an append query to copy the data from the source into the target.
Owner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010
Commented:
Actually, you can create what many call an UPSERT query, which updates data where it already exists, and appends where it doesn't.  The syntax for that is something like:
UPDATE  (
SELECT Field1, Field2, Field3
FROM SomeTable
)  AS ST 
LEFT JOIN DestinationTableName as DT 
ON ST.Field1 = DT.Field1   
SET DT.Field1 = ST.Field1
, DT.Field2 = ST.Field2
, DT.Field3 = ST.Field3

Open in new window

You will note that I have a subquery to select the values that I want to insert into the destination table.  You need to make sure that this SELECT subquery contains all of the fields you need to ensure that the records in the recordset you want to update/append from match with those in the destination table

NOTE: Make sure you backup your database before you attempt this.
Distinguished Expert 2017

Commented:
Do you update the data in the database by any other method except the spreadsheet?  If the answer is yes, then how do you propose to determine which version of the record, field by field, is current?
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
Yes you can. Follow the few steps here:

Combined "update or append" query

Author

Commented:
Thanks for the help!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial