2012年5月13日 星期日

用 EXCEL VBA QueryTables 物件向 Access 取得資料

QueryTables 物件的 ADD 方法有三個參數,分別為 Connection、Destination 與 Sql。

Microsoft 的官方說明如下:

Creates a new query table. Returns a QueryTable object that represents the new query table.
expression.Add(Connection, Destination, Sql)expression Required. An expression that returns a QueryTables object.
Connection Required Variant. The data source for the query table. Can be one of the following:
  • A string containing an OLE DB or ODBC connection string. The ODBC connection string has the form "ODBC;".
  • A QueryTable object from which the query information is initially copied, including the connection string and the SQL text, but not including the Destination range. Specifying a QueryTable object causes the Sql argument to be ignored.
  • An ADO or DAO Recordset object. Data is read from the ADO or DAO recordset. Microsoft Excel retains the recordset until the query table is deleted or the connection is changed. The resulting query table cannot be edited.
  • A Web query. A string in the form "URL;", where "URL;" is required but not localized and the rest of the string is used for the URL of the Web query.
  • Data Finder. A string in the form "FINDER;" where "FINDER;" is required but not localized. The rest of the string is the path and file name of a Data Finder file (*.dqy or *.iqy). The file is read when the Add method is run; subsequent calls to the Connection property of the query table will return strings beginning with "ODBC;" or "URL;" as appropriate.
  • A text file. A string in the form "TEXT;", where TEXT is required but not localized.
Destination Required Range. The cell in the upper-left corner of the query table destination range (the range where the resulting query table will be placed). The destination range must be on the worksheet that contains the QueryTables object specified by expression.
Sql Optional Variant. The SQL query string to be run on the ODBC data source. This argument is optional when you're using an ODBC data source (if you don't specify it here, you should set it by using the Sql property of the query table before the table is refreshed). You cannot use this argument when a QueryTable object, text file, or ADO or DAO Recordset object is specified as the data source.

先前,我都是用 ADO 物件向 Access 索取資料。
每次使用 ADO 總是要先宣告許多 ADODB 變數,如: Connection 、 Recordset 等,先建立 Connection 然後再用 Execute 方法把資料送給 Recordset ,再用 EXCEL 的 CopyFromRecordset 把資料塞到儲存格中,每次程式碼都寫得很長,造成閱讀或維護的困難。

前幾天在 MSDN 上查閱 QueryTables 物件的說明,其中這句「A string containing an OLE DB or ODBC connection string. The ODBC connection string has the form "ODBC;".」讓我萌生用 QueryTables 向  Access 索取資料的想法。

我嘗試以 EXCEL 2010 向  Access 2003 所建立的 MDB 檔案索取資料,試驗的結果非常成功。不論用 ODBC 還是 OLE DB 都能夠順利取得資料。
更棒的是,可以用 QueryTables 物件的 SQL 參數,以 SQL 語法取得想要的資料。
程式碼如下:
ODBC 法:

With ActiveSheet.QueryTables.Add( _
    Connection:="ODBC;Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\myDB.mdb;uid=Admin;Pwd=", _
    Destination:=Sheet1.Range("A1"), _
    Sql:="SELECT * FROM Tbl_l Where Col_1=x")
        .Refresh
End With

OLE DB 法:

With ActiveSheet.QueryTables.Add( _
    Connection:="OLEDB;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\myDB.mdb;User Id=Admin;Password=, _
    Destination:=Range("A1"), _
    
Sql:="SELECT * FROM Tbl_l Where Col_1=x") 
        .Refresh
End With

P.S. 1 其中 Connection 參數的語法為: ODBC; 或 OLEDB;
P.S. 2 其中 根據資料庫類型 (SQL Server, Access, Oracle) 的不同而有不同的指令,可以去 ConnectionStrings.Com 查。

2012/5/14 Update:
很遺憾的,QueryTables 物件 ADD 方法的 Sql 參數無法接受「參數查詢」。但是可以用 VBA 結合字串的方式解決。

沒有留言:

張貼留言