文章編號: 147739 - 上次校閱: 2006年10月11日 - 版次: 2.3

如何從 MS Access 使用 DAO 擷取一個 QueryDef

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。
全部展開 | 全部摺疊

結論

在 Microsoft Excel 版本 97 與 7.0 您可以使用 Visual Basic 應用程式中的資料存取物件 (DAO) 從 Microsoft Access 擷取 QueryDef 結果本文將告訴您,如何執行這項操作。

其他相關資訊

Microsoft 僅,為了說明提供程式設計範例,不提供任何明示或默示的保證。這包括,但不限於適售性或適合某特定用途之默示擔保責任。本文假設您已熟悉使用我們所示範的程式設計語言以及建立和偵錯程序所使用的工具。Microsoft 技術支援工程師可以協助解釋特定程序的功能,但它們不會修改這些範例以提供附加功能或建構程序,以符合您特定需求。本文使用 Microsoft Office 95 專業人員所隨附的北風貿易資料庫。如果您選取 [預設選項資料庫位於 \Msoffice\Access\Samples 資料夾中。如果 Northwind 資料庫位於不同的資料夾,編輯所提供的程式碼,執行它之前。

在 Microsoft Office 97 Northwind 資料庫的預設路徑是 \Program Files\Microsoft Office\Office\Samples\Northwind.mdb。

一個 QueryDef 在 Microsoft Access 中建立,且與資料庫檔案一起儲存。 查詢所組成,並可能或可能不包含準則。

注意: 如果您收到 「 使用者定義類型未定義 」 錯誤,啟動模組工作表、 按一下 [工具] 功能表上的 [參考,按一下 Microsoft DAO 3.0 物件程式庫] 核取方塊。

從 Microsoft Access 擷取一個 QueryDef 結果是五個步驟的程序,如下所示:
  1. 建立一個資料庫物件。
  2. 建立一個 QueryDef 物件。
  3. 建立資料錄集物件。
  4. 擷取標頭檔 (如果需要的話)。
  5. 從資料表擷取資料。
擷取資料之後應關閉由發出.Close 命令開啟的所有物件。
   Sub GetQueryDef()
       'This sub will get data from an Existing QueryDef in the Northwind
       'database and place the data on sheet2.

         Dim Db As Database
         Dim Qd As QueryDef
         Dim Rs As Recordset
         Dim Ws As Object
         Dim i As Integer
      Dim Path as String

      'Set the Path to the database. This line is useful because
      'if your database is in another location, you just need to change
      'it here and the Path Variable will be used throughout the code.
      '
      'If you're using Microsoft Office 97, the line should read:
      '
      'Path = "C:\Program Files\Microsoft
      'Office\Office\Samples\Northwind.mdb"
      '
      Path = "C:\Msoffice\Access\Samples\Northwind.mdb"

       'Set Ws
       Set Ws = Sheets("Sheet1")

       'This set of code will activate Sheet1 and clear any existing data.
       'After clearing the data, it will select cell A1.
       Ws.Activate
       Range("A1").Activate
       Selection.CurrentRegion.Select
       Selection.ClearContents
       Range("A1").Select

       'Set the Database and QueryDef. This QueryDef exists in the
       'database.
       Set Db = Workspaces(0).OpenDatabase(Path, ReadOnly:=True, _
         Exclusive:=False)
       Set Qd = Db.QueryDefs("Invoices")

       'Create a new Recordset from the Query based on the stored
       'QueryDef.
       Set Rs = Qd.OpenRecordset()

       'This loop will collect the field names and place them in the first
       'row starting at "A1."
       For i = 0 To Rs.Fields.Count - 1
           Ws.Cells(1, i + 1).Value = Rs.Fields(i).Name
       Next

      'This line simply sets the font to bold for the headers.
      Ws.Range(Ws.Cells(1, 1), Ws.Cells(1, Rs.Fields.Count)).Font.Bold _
          =True

      'The next line will get the data from the recordset and copy it
      'into the Worksheet (Sheet1).

       Ws.Range("A2").CopyFromRecordset Rs

      'This next code set will just select the data region and auto-fit
      'the columns
       Sheets("Sheet1").Select
       Range("A1").Select
       Selection.CurrentRegion.Select
       Selection.Columns.AutoFit
       Range("A1").Select

       Qd.Close
       Rs.Close
       Db.Close

   End Sub
				

?考

如需詳細資料存取物件的相關,資訊從 Visual Basic 編輯器] 中,按一下 [Office 小幫手],鍵入"DAO"按一下搜尋,然後再按一下,檢視 「 資料存取物件概觀 >。

注意: 如果隱藏 [小幫手] 按一下 [一般] 工具列上的 [Office 小幫手] 按鈕。如果小幫手無法回答您的查詢,請參閱下列的 「 Microsoft 知識庫 」 中的文件:
176476? (http://support.microsoft.com/kb/176476/EN-US/ ) OFF: Office 小幫手無法回答 Visual Basic 問題
如需有關 Microsoft Excel 版本 7.0 中的資料存取物件的詳細資訊,按一下 Microsoft Excel 說明] 和 [型別中的 [索引] 索引標籤:
DAO

這篇文章中的資訊適用於:
  • Microsoft Excel 97 Standard Edition
  • Microsoft Excel 95 Standard Edition
關鍵字:?
kbmt kbdtacode kbhowto kbprogramming KB147739 KbMtzh
機器翻譯機器翻譯
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。
按一下這裡查看此文章的英文版本:147739? (http://support.microsoft.com/kb/147739/en-us/ )
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。
Retired KB Article依現狀不再更新的知識庫內容免責聲明
本文旨在說明 Microsoft 不再提供支援的產品。因此,本文係依「現狀」提供,不會再更新。