Ask Your Question
4

How can I use SQL and Schema.ini to import a CSV file into a new table in MS Access VBA while ensuring that the column names of the table are enforced?

asked 2023-07-21 12:36:05 +0000

bukephalos gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-07-21 13:06:02 +0000

plato gravatar image

You can use the following steps to import a CSV file into a new table in MS Access VBA while ensuring that the column names of the table are enforced:

  1. Create a Schema.ini file: A Schema.ini file is a text file that contains information about the format and layout of the CSV file. It includes details like the data type and width of each column. You can use a Schema.ini file to ensure that the column names are enforced during the import process.

  2. Create a new table in MS Access: In the MS Access database, create a new table with the same structure as the CSV file. Ensure that the column names in the table match the column names in the CSV file.

  3. Use SQL to execute the import: In VBA, use SQL to execute the import process. Here is an example:

Dim sql As String

sql = "INSERT INTO MyNewTable SELECT * FROM [Text;FMT=Delimited;HDR=Yes;Database=C:\MyFolder].[MyCSVFile.csv];"

DoCmd.RunSQL sql

This will insert all the data from the CSV file into the new table. The [Text;FMT=Delimited;HDR=Yes;Database=C:\MyFolder].[MyCSVFile.csv] part of the SQL statement tells MS Access to treat the CSV file as a delimited file with a header row.

  1. Run the import process: Run the SQL statement to execute the import process. The data from the CSV file will be added to the new table in MS Access, with the column names enforced.
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-21 12:36:05 +0000

Seen: 16 times

Last updated: Jul 21 '23