0

我正在尝试编写一个代码来从 MS Access 数据库中导入数据,每次我运行宏时我都会收到这个错误,我无法弄清楚代码有什么问题。你能告诉我错误是什么吗:

 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
4

1 回答 1

0

Access uses square-brackets [ ] in place of the back-ticks that other databases use (MySQL).

Date is also a reserved word in Access and should also be enclosed in square brackets or, much better!, rename this field.

BTW It is hard to read, but it looks like all your data is coming from a single table INNW_CFC_1X, so you don't have to repeat this table-name for all the fields.

于 2013-07-15T21:55:16.937 回答