Create a new lookup table where data already exist

2019-04-13 08:20发布

问题:

I am working on a database in MS Access 2013 which has a considerable amount of non-normalised data, and I want to move them out to alternate tables and use them as lookups in the main table. However, when I create a lookup column, MS Access deletes the data and there is far too much data to reset every record by hand.

Is there a way in Access 2013 to create such a lookup without losing the data?

Please don't comment about how using lookup tables in Access is bad. I have read posts like the one below and I disagree with most of the points there, and some of them are just simply wrong.

http://access.mvps.org/access/lookupfields.htm

Below is a sample of my data. I need to extract the 2nd and 3rd fields to other tables. If I can do this with them, I can do it with the others.

Presently this is stored as text in the fields. I would like to remove them and replace them with FK id's.

回答1:

You could create your second table and add the data to the table. Then update the first table to match the records to each other

Let say you have the following table:

CustOrders
ID       Customer     DateOrdered
123      K-Mart       01/01/2013
124      K Mart       01/05/2013
125      Walmart      02/05/2013
126      Walmart      03/07/2013
127      Miejers      03/11/2013
128      K-Mart       03/12/2013

You could find out all of the Customers that are in the CustOrders table by performing the following:

SELECT DISTINCT Customer From CustOrders

Then create a record in the following table for each:

Customers
ID       Customer
1        K-Mart
2        Walmart
3        Miejers

Then you could Update the CustOrders table by performing the following:

UPDATE CustOrders SET Customer = 1 WHERE Customer = 'K-Mart' OR Customer = 'K Mart'

Of course you would have to do this for each distinct customer you have.

Then if you want you could change the data type of the Customer field in the CustOrders table to Long Integer.

Lastly I would create a combo box on any entry/edit form that has the following RowSource:

SELECT ID, Customer FROM Customers ORDER BY Customer

Set the combo box to limit from list and bind to column 1.