Importing is usually an error-prone process. Even if the CSV is generated by
other software, you still have potential issues so sort out. Things like:
- problems with currency symbols
- problems with dates
- zero-length strings instead of nulls,
- trailing spaces,
- validation rules not met,
- text that needs to be converted to numbers for foreign keys,
- foreign key values need to be added to a lookup table before import,
- line-wraps in the wrong places,
- duplicates of existing entries,
- users importing the same file multiple times,
- needing to write to multiple tables (e.g. Orders header entry, then
details),
- etc, etc.
To sort out this mess, what I find I normally have to do is to create a
temporary import table with just Text columns. Then run a series of queries
to test for all the invalid things you consider likely. You write each issue
to another table, flagging each issue as Critical or Warning. The interface
lets the user import the file (into the temp table), performs the analysis,
handles the lookups, displays the problems in a list box, and offers an
interface into the temp table for editing. Once all critical problems are
handles, you enable the command button for the final step which adds the
data to your real tables.
This final button creates a record in an ImportBatch table, which is
structured so you can recogize if the file has already been imported (same
file name and file date/time.) You then execute a series of append query
statements to write the data to your real tables, inside a transaction so
you can rollback if there was an unforeseen error. The real target table has
a BatchID foreign key. Use that key value to identify where the import file
name and date, and the user who performed the import.
The BatchID foreign key means you can also offer an "Undo last import"
option.
Suggested ImportBatch table fields:
ImportBatchID AutoNumber
ImportFile Text (full path and file name)
ImportFileDateTime Date/Time (date/time of the file)
ImportBy Text (user name)
ImportDateTime Date/Time (when imported)
Hope you can adapt those ideas to your specific needs.