Discussion Locked

Updating Access from Excel

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I have no problem adding new records to a customer database from an Excel spreadsheet using the import function. However, what I would like to do is to add data to an existing record in a table. My Excel spreadsheet column headings reflect the table field names, and I have tried entering the primary key of an existing record together with data in the field I want to update (such as a sales figure). Import doesn't like it as it tries to add as a new record and obviously finds a duplicate key. Is there a way to handle these sorts of batch updates?

5 replies

by roc180

Import the data from Excel into an empty new table (TBL_New) then INNER JOIN the the Existing Table (TBL_EX)on primary key and run an UPDATE query to update TBL_EX with any data from TBL_New. This takes care of updating existing records with new data, now run an APPEND query LEFT JOIN Tbl_New to TBL_EX on primary keys WHERE TBL_EX.Primary_Key = NULL this will append any NEW records to your TBL_EX. If you want an ACCESS97 example of what I just described email me and let me know, I would be happy to pass it on. Good Luck. Neal Martinelli

by Member 18636374

If you have repetitive updates, macro's work well. Keep your primary keys the way you have them. Open the database and click on the Macro tab and click on NEW. Click in the first row under the ACTION column and pick from the drop down box. Fill infive or six rows under the action column. Fill in:

SetWarning
TransferSpreadsheet
RunSQL
RunSQL
DeleteObject
Close

SetWarning - Use this to eliminate Access warnings when the macro runs and the duplicate records will not append. - Use the Action Arguments box at the bottom of the screen to set Warnings On to NO. This row is optional but a nice thing to set when you know the macro is working well.

TransferSpreadsheet - Use the Action Arguments box at the bottom of the screen to set the action to IMPORT, set the spreadsheet TYPE, give the new table that is being created a NAME, set the full path to include the spreadsheet name (EX: C:\Directory\spreadsheet.xls), and set field names to YES.

Go to the query tab and set-up two queries as if you were going to do the operation manually but don't run them. Then, while in the query window, go to view on the menu bar and select SQL view. Copy the SQL code.

RunSQL - In the Action Arguments of the first RunSQL, paste the code for the append query

RunSQL - In the Action Arguments of the second RunSQL, paste the code for the update query

DeleteObject - In the Action Arguments of DeleteObject, set the Object to TABLE and the Object Name to the name you gave the 'new imported table' in TransferSpreadsheet

Close - In the Action Arguments of Close, set the Object Type to TABLE, the Object Name to the name of the table you appended to, and Save to YES. This setting is optional but it is a nice touch to finish the macro off.

Close and save the Macro and you are done. It is a good policy to use Tools \ Database utilities \ Compact after you run the macro to reclaim HD space.

This discussion is closed to new replies.