我正在尝试构建一个宏来帮助更新我的ODBC连接。我在手动操作时运行了宏录制器,但生成的代码给出了
运行时错误'1004':
应用程序定义或对象定义的错误
以下是代码的简化代码段:
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
这是宏录制器给我的要点 - 除了我的查询(.CommandText
)相当长一点。此外,file.csv实际上有一个日期组件,我将其建立为变量,由宏开头的InputBox设置。
错误出现在SQL部分(.CommandText
)。这是在我甚至从录音机的输出做出任何改变之前。