How to assign foreign keys in Access within imported table from Excel

你离开我真会死。 提交于 2019-12-12 04:34:38

问题


I will use Access database instead of Excel. But I need to import data from one huge Excel sheet into several pre-prepared normalized tables in Access. In the core Access table I have mainly the foreign keys from other tables (of course some other fields are texts or dates).

How should I perform the import in the easiest way? I cannot perform import directly, because there is NOT, for example, "United States" string in the Access field 'Country'; there must be foreign key no. 84 from the table tblCountries. I think about DLOOKUP function in the Excel and replace strings for FK... Do you know any more simple method?

Thank you, Martin


回答1:


You don’t mention how you will get the Excel data into several Access tables, so I will assume you will import the entire Excel file into ONE large table then break out the data from there. I assume the imported data may NOT match with existing Access keys (i.e. misspellings, new values, etc.) so you will need to locate those so you can make corrections. This will involve creating a number of ‘unmatched queries’ then a number of ‘Update queries’, finally you can use Append queries to pull data from your import table into the final resting place. Using your example, you have imported ‘Country = United States’, but you need to relate that value to key “84”?

Let’s set some examples:

  1. Assume you imported your Excel data into one large Access table. Also assume your import has three fields you need to get keys for.
  2. You already have several control tables in Access similar to the following:

a. tblRegion: contains RegionCode, RegionName (i.e. 1=Pacific, 2=North America, 3=Asia, …)

b. tblCountry: contains CountryCode, Country, Region (i.e. 84 | United States | 2

c. tblProductType: contains ProdCode, ProductType (i.e. VEH | vehicles; ELE | electrical; etc.)

d. Assume your imported data has fields

Here are the steps I would take:

  1. If your Excel file does not already have columns to hold the key values (i.e. 84), add them before the import. Or after the import, modify the table to add the columns.
  2. Create ‘Unmatched query’ for each key field you need to relate. (Use ‘Query Wizard’, ‘Find Unmatched Query Wizard’. This will show you all imported data that does not have a match in your key table and you will need to correct those valuse. i.e.:

    SELECT tblFromExcel.Country, tblFromExcel.Region, tblFromExcel.ProductType, tblFromExcel.SomeData FROM tblFromExcel LEFT JOIN tblCountry ON tblFromExcel.[Country] = tblCountry.[CountryName] WHERE (((tblCountry.CountryName) Is Null));

  3. Update the FK with matching values:

    UPDATE tblCountry INNER JOIN tblFromExcel ON tblCountry.CountryName = tblFromExcel.Country SET tblFromExcel.CountryFK = [CountryNbr];

  4. Repeat the above Unmatched / Matched for all other key fields.



来源:https://stackoverflow.com/questions/26798867/how-to-assign-foreign-keys-in-access-within-imported-table-from-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!