我正在尝试编写一个代码来从MS访问数据库导入数据,每次运行宏时我都会遇到此错误,我无法弄清楚代码有什么问题。你能告诉我错误是什么:
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
"ODBC;DSN=MS Access Database;DBQ=" & db_fullname & ";DefaultDir=" & db_dir & ";DriverId=25;FIL" _
), Array("=MS Access;MaxBufferSize=2048;PageTimeout=5;")), Destination:=Range( _
"$A$1")).QueryTable
.CommandText = Array( _
"SELECT INNW_CFC_1X.Date_Cascade_Sector_Band, INNW_CFC_1X.Cascade_Sector_band, INNW_CFC_1X.Date, INNW_CFC_1X.Market, INNW_CFC_1X.`BSC Name`, INNW_CFC_1X.`Cluster ID`, INNW_CFC_1X.`Cascade ID`, INNW_CFC" _
, _
"_1X.`BTS ID`, INNW_CFC_1X.Sector, INNW_CFC_1X.Band, INNW_CFC_1X.cfc18, INNW_CFC_1X.cfc30, INNW_CFC_1X.cfc53, INNW_CFC_1X.`Total RF CFC`, INNW_CFC_1X.cfc84, INNW_CFC_1X.cfc85, INNW_CFC_1X.`Total Capaci" _
, _
"ty CFC`, INNW_CFC_1X.cfc26, INNW_CFC_1X.cfc49, INNW_CFC_1X.cfc102, INNW_CFC_1X.cfc2, INNW_CFC_1X.cfc54, INNW_CFC_1X.cfc99, INNW_CFC_1X.`Total BH CFC`, INNW_CFC_1X.cfc31, INNW_CFC_1X.cfc29, INNW_CFC_1X" _
, _
".cfc81, INNW_CFC_1X.`Total Parameter CFC`, INNW_CFC_1X.cfc16, INNW_CFC_1X.cfc17, INNW_CFC_1X.`Total IVHHO CFC`" & Chr(13) & "" & Chr(10) & "FROM `" & db_fullname & "`.INNW_CFC_1X " & Chr(13) & "" & Chr(10) & "WHERE (INNW_CFC_1X.Date>={ts '" & start_date2 & " 00:00:00'} And INNW_CFC_1X.Date<={ts '" & end_date2 & "" _
, _
" 00:00:00'}) " & query_string & "")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Table_Query_from_MS_Access_Database"
.Refresh BackgroundQuery:=False
End With
答案 0 :(得分:0)
Access使用方括号[]代替其他数据库使用的反向标记(MySQL)。
日期在Access中也是reserved word,也应该用方括号括起来,或者更好!重命名此字段。
BTW这很难读,但看起来你的所有数据都来自一张表INNW_CFC_1X,所以你不必为所有字段重复这个表名。