Update Foreign Keys

Posted on 2013-09-15
Medium Priority
Last Modified: 2013-09-16
I have table Orders with an ID autonumber field


I have table Detail with a field 'OrderID' which points to ID field in table Orders.


One or Many records in table Detail point to a single Order record.

The ID's in table orders have to be changed/resequenced which in turn means that the 'OrderID' fields in table Detail must now point to the new 'OrderID'.

I have created a new table Order_Reseq and appended all of the order records to it.  The append query appends the ID field in the original Order table to a field called OldID in the table Order_Reseq.  The fields in Order_Reseq are:

ID                 which is an autonumber
OldID           which is the ID from the original Orders table

Now I must change all of the 'OrderID' fields in table Detail to point to the new ID in 'Orders_Reseq'.

One idea I have is to loop thru table Orders_Reseq record by record.  For each record I would execute and update/set query on table Detail revising 'OrderID' to the value of 'ID' in table Orders_Reseq' for every record

Read thru Order_Reseq record by record

updateString = " Update tblDetail " & _
                       " Set [OrderID]      = " & rsIn!ID & _
                       " Where [OrderID] = " & rsIn!OldID
DoCmd.RunSQL updateString

I am thinking there must be a better way, so that is the question.   Is there an easier more efficient way of revising all of the 'OrderID' fields in to the revised ID in the orders table?

Efficienty is important because their are approximatley 3 million records in the order table and 8  millionn in the Detail table.
Question by:mlcktmguy
LVL 25

Accepted Solution

chaau earned 1000 total points
ID: 39495113
The easiest way to do the updates is to enforce cascade updates/deletes. In this case you would only need to update/delete in the master table. The dependant tables will be updated automatically. To do so, create the relationship and check the cascade checkboxes, as per below:
 cascade update
LVL 50

Expert Comment

by:Dale Fye
ID: 39495881
However, your comment:

"The ID's in table orders have to be changed/resequenced"

has me somewhat concerned.  Autonumber fields are just that (auto); you should rarely present these values to your customers or users as they can be confusing, and rarely is there a good reason to "change/resequence" them.  

If you need a number that you can present to your users (Case #, Invoice #, ...) then I would recommend creating another field and computing that value based on your internal rule set (generally via a function).  It gives you more flexibility to make changes later on, and still allows you to use the autonumber as the "behind the scenes" link between your various tables.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
If you need to implement application level security in an Access database application or other VBA code, I strongly encourage you to take advantage of Active Directory groups.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question