Importing Excel Data into Access Using Index - How

2019-07-24 03:01发布

问题:

I am trying to use an Index to import monthly data from Excel into a Table (by Appending new records to the Table).

I have created a unique index in the Table being posted to to prevent the import of duplicate records.

However when I click import (and assuming the records already exist) it still gives the user the option to import the same records again by clicking yes to the message below:

How would I get the macro to not run if the entries already exist and also not give the user the above message option?

I have tried suppressing messages temporarily using a bit of code as referred to below, but that just processes the import anyway and re-adds the entries to the table again.

Thanks

回答1:

Excel imports often have issues (even if you haven't found any in this particular file), expecially if the Excel file is created or edited by humans. My suggestion would be to import the file into a temporary table consisting of all text fields (F1, F2, etc) and an identity field. Using all text fields, all data should import without any conversion problems. Don't use the first row as column heads. Import it as the first row of data. That way you can easily check for correct column headings. If correct, just delete that row.

Then validate and scrub the data using queries and/or code against the temporary table to identify any data that does not meet the requirements of your permanent tables. E.G. Select * From WHERE Not IsNumeric(F3).

Then remove any leading or trailing spaces, double spaces, etc. and whatever other cleanup you might like to do.

Then use a query with an outer join (on your PK fields) to append rows from the temp table, that are not already in the permanent table, to the permanent table.