Update table information via update query?

spudracer

Here and there
Local time
Today, 16:36
Joined
Jul 1, 2008
Messages
199
First of all, I don't know if this is possible.

I've got two tables that a query will pull information from for a form. One, the information doesn't change, the other gets updated roughly every week via a download of an Excel spreadsheet.

I want to merge the two tables into one, but the only thing preventing this is the fact that one table is constantly updated.

What type of query can I create to help me with this? Allow the fields that come from the downloaded file to still be updated, after the two tables have merged.

Would it be an update? Would it be an Append? This aspect of Access has never made sense to me. Any and all help is appreciated on this.
 
I would leave them as separate table because of the update. Can you join them in a query? Do they have a common field?
 
Yeah, I guess I can leave them separate. I found a database built by someone else using a macro and the TransferDatabase option, so I figured out part of my question.

I don't know that doing that will replace the information that's already in the table, or if it will just add-on. Which brings me back to my original question...

The table that relies on an imported spreadsheet is updated, let's say once a week. The information contained in this table is the person's name, their department and other miscellaneous information. Say two people out of the 100 people change their phone numbers. When I go to do the next import, I want any information that hasn't changed to not be removed or re-added, but the two phone numbers that changed to be the only things that change.

Is that possible via a Macro running a query? If so, which type of query would I use? Is there site that has a very indepth tutorial explaining something like this?
 
I don't know that doing that will replace the information that's already in the table, or if it will just add-on.
A basic docmd.transfer will only ADD data, not change/update/delete, unless you do it yourself.

Say two people out of the 100 people change their phone numbers.
I would do it like so...

1) Docmd.Transfer into a temporary table.
2) Make a query to delete all employee's that exist in the Temp Table.
3) Make an append query to re-add all empleyee's you just removed

Atleast that is the quickest way... Better would be to update everything that needs updating, delete what needs removing (think about referential integrity!), and add that which needs adding. Though this is much more involved, it is slightly better. Above 1,2,3 is the Q&D solution.

Is that possible via a Macro running a query?
NEVER ever ever use a macro to do anything semi serious! NEVER!
Macro's are NOT very flexible and you will run into problems with them fast, just do "real" coding instead.

Finaly if you have 2 seperate parts of information, with seperate dependancies/sources it is ussually best to store them in seperate tables, as I think it will be in this case.
 
i do a similar thing to mailman,

ie i have a real table, and bring the update into a temporary table, but i dont delete the old data first

i use an unmatched query to add any new records to the real table
and then i run an update query to update the volatile data previously loaded to the real table (addresses, phone numbers etc)

the reason i dont delete the real data, is that this data is probably needed to maintain relational integrity IN the database. However the users may have deleted some of these items from their external data (eg leaving employees) - but i dont want to delete them from the real data table

it can still become a problem if the dolts then reuse the deleted references.
 
A basic docmd.transfer will only ADD data, not change/update/delete, unless you do it yourself.


I would do it like so...

1) Docmd.Transfer into a temporary table.
2) Make a query to delete all employee's that exist in the Temp Table.
3) Make an append query to re-add all empleyee's you just removed

Atleast that is the quickest way... Better would be to update everything that needs updating, delete what needs removing (think about referential integrity!), and add that which needs adding. Though this is much more involved, it is slightly better. Above 1,2,3 is the Q&D solution.


NEVER ever ever use a macro to do anything semi serious! NEVER!
Macro's are NOT very flexible and you will run into problems with them fast, just do "real" coding instead.

Finaly if you have 2 seperate parts of information, with seperate dependancies/sources it is ussually best to store them in seperate tables, as I think it will be in this case.

Understand not to use the Macro for running the query. My problem lies in the fact that the coding is a foreign language to me, I've never been able to understand it. Would you be willing to post an example of what you mean? I would probably understand all of this much better that way... :D

Again, thanks for your help!
 
Several issues to be addressed:

First, you have separate tables for your data. Why? (This is rhetorical.) If you split it only because of separate data sources, it makes sense to merge the data, but if you split the data because of normalization, your method of populating the table(s) shouldn't change. Separation for normalization won't change regardless of your data sources.

Second, finding differences between two iterations of the same imported table can be a bear when users have access to the table source. For that reason, I would never allow direct import from the spreadsheet. It will need some kind of massaging. Therefore, suggestions to import to an intermediate table followed by some sort of cleansing are spot-on.

Third, is there any need for historical tracking of changes? Because this situation and possibility just leaps out of the description you gave us.

I might take the approach of importing the updated spreadsheet to a table that has columns for everything in the spreadsheet that you need to keep plus one more column, type Yes/No, titled "UPDATEIT." Rather than importing the data to a new table, import it to an existing table with the correct field structure already. Just delete every record in the table before importing so that it is empty, then import. If necessary, go back and set the UPDATEIT field to NO with an UPDATE query on your temp table, where that query has no WHERE clause.

Then what you do is, you write an update query joining the temp table to the existing table on the field that you are using as the prime key. You update the UPDATEIT flag to YES if any of the critical fields in the temp table do not match the corresponding fields in the tables you have from last week.

If you need to do this step in several update queries, one for each pair of fields to be tested, that's OK. It might run slower but it would be easier to maintain and understand. If you also need to consider flagging records that don't appear in last week's table but DO appear in this week's table, again set UPDATEIT to YES. You might also do a pre-scan to see if there are entries in the temp table that would fail a test of relational integrity because they don't correspond to records in your more-or-less fixed table you mentioned.

When you are done, the UPDATEIT flag will be YES or NO depending on whether any differences were found. For the records in the temp table only, DELETE records via a DELETE query selective for UPDATEIT = NO. What's left in the temp table is what needs to be updated in the "real" table.

You could use a macro to run several update queries in a row, but I'm thinking you get better control (and better error handling) if you do it this way: First write your macro to run those update queries. Then convert the macro to VBA and add error handlling to the result. It's a quick and dirty shortcut, but then I've been accused of being a quick and dirty guy who CAN type but doesn't always like to. Converting a macro to VBA is one of those shortcuts that makes initial design easy, but then makes subsequent implementation of extra error checking possible.

Then you can trigger the operation in some other way, perhaps from a form with a control button that runs the code you converted. In VBA context, you can trap errors and look at intermediate data if necessary. Also make log entries showing whatever you found or whatever you rejected as "no change" or however that needs to work.

This might be daunting, but it is DEFINITELY a case where Julius Caesar got it right: Divide and conquer. Break this problem up into little pieces, because each of the pieces is eminently do-able.
 

Users who are viewing this thread

Back
Top Bottom