Pull .csv file into Access Database
I have a case where I need to pull .csv files into an Access database. I need to keep the data in its current structure. There could be multiple .csv files, so I need to make the tables 'on the fly'.
I'm trying the code below, but the error message I'm receiving (on the execute line) is: 'Query input must contain at least one query or table'
Just a quick note or two: The connection does open, and as it is a .csv file, it gets fed the path to that开发者_JS百科 file not the file itself (as you'll see). The .csv does have rows, I can do a 'Select * From tocopy.csv' and that works as expected.
Code I'm trying:
Dim ConnectionString As String
ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\test\;Extended Properties=""Text;HDR=Yes"""
Dim myConnection As New System.Data.OleDb.OleDbConnection(ConnectionString)
myConnection.Open()
Dim cmd As New System.Data.OleDb.OleDbCommand
Dim sSQL As String = ""
sSQL = "SELECT * INTO [MS Access;Database=C:\test\intake.mdb].[DynamicTable] FROM [tocopy.csv]"
cmd.CommandText = sSQL
cmd.Connection = myConnection
cmd.ExecuteNonQuery()
myConnection.Close()
myConnection = Nothing
Do you really have the need to import several CSV files with completely different column structure into your database? In what way will you process the imported data then?
If the structure of the data is quite irrelevant, because you don't run queries on it, try to import the lines from the CSV files into one table with just one field of type MEMO, and then process these records with some VBA code.
精彩评论