I am trying to build a macro to help update my ODBC connection. I ran the macro recorder while I did it manually, but the code gives a
Below is a simplified snippet of the code:
That's the gist of what the macro recorder gave me - except my query (<code>.CommandText</code>) is quite a bit longer. Also, the file.csv actually has a date component to it, which I am establishing as a variable, set by an InputBox at the beginning of the macro.
The error comes about at the SQL section (<code>.CommandText</code>). This is before I even make any changes from the recorder's output.
Any help is much appreciated.
Run-time error '1004':
Application-defined or object-defined error
Below is a simplified snippet of the code:
Code:
With ActiveWorkbook.Connections("ConnectionName"). _
ODBCConnection
.BackgroundQuery = False
.CommandText = "SELECT * FROM C:\dir\file.csv"
.CommandType = xlCmdSql
.Connection = Array(Array( _
"ODBC;DBQ=C:\dir;DefaultDir=C:\dir" _
), Array( _
" ;Driver={Microsoft Text Driver (*.txt; *.csv)};DriverId=27;Extensions=None,asc,csv,tab,txt;FIL=text;MaxBufferS" _
), Array( _
"ize=2048;MaxScanRows=8;PageTimeout=5;SafeTransactions=0;Threads=3;UID=admin;UserCommitSync=Yes;" _
))
.RefreshOnFileOpen = False
.SavePassword = False
.SourceConnectionFile = ""
.ServerCredentialsMethod = xlCredentialsMethodIntegrated
.AlwaysUseConnectionFile = False
End With
With ActiveWorkbook.Connections("ConnectionName")
.Name = "ConnectionName"
.Description = ""
End With
ActiveWorkbook.Connections("ConnectionName").Refresh
The error comes about at the SQL section (<code>.CommandText</code>). This is before I even make any changes from the recorder's output.
Any help is much appreciated.