Microsoft Access 以目標(客戶端)應用程序或源(服務器)應用程序形式支持動態(tài)數據交換 (DDE)。例如,一個作為客戶端的應用程序,如 Microsoft Word,可以通過 DDE 從一個作為服務器的 Microsoft Access 數據庫請求數據。
客戶端和服務器之間的 DDE 對話是建立在特定的主題上的。主題可以是服務器應用程序所支持格式的數據文件,也可以是提供有關服務器應用程序本身信息的 System 主題。開始了特定主題的對話后,只能傳送與該主題相關的數據項。
例如,假定正在運行 Microsoft Word,并要將一個特定的 Microsoft Access 數據庫中的數據插入到文檔中,通過使用 DDEInitiate 函數將 DDE 通道打開,并將數據庫文件名稱指定為主題,即可開始與 Microsoft Access 的 DDE 對話。然后通過該通道即可將數據庫中的數據傳給 Microsoft Word。
作為 DDE 服務器,Microsoft Access 支持以下主題:
? | System 主題 |
? | 數據庫名稱 (database 主題) |
? | 表名稱 (tablename 主題) |
? | 查詢名稱 (queryname 主題) |
? | Microsoft Access SQL 字符串(sqlstring 主題) |
建立了 DDE 對話后,可以使用 DDEExecute 語句將命令從客戶端發(fā)送到服務器應用程序。在用作 DDE 服務器時,Microsoft Access 將以下命令均識別為有效命令:
? | 當前數據庫中的宏名稱。 |
? | 在 Visual Basic 中,通過使用 DoCmd 對象的某個方法可以執(zhí)行的任何操作。 |
? | 只為 DDE 運算而使用的 OpenDatabase 和 CloseDatabase 操作(有關如何使用這些操作的信息,請參閱該主題以后的示例)。 |
注釋 當將一個宏操作指定為 DDEExecute 語句時,該操作和所有參數均遵循 DoCmd 對象的語法且必須包含在方括號([ ])中。但支持 DDE 的應用程序并不識別 DDE 運算中的固有常量。同樣,如果字符串包含逗號,則必須將該字符串參數包括在引號 (" ")中。否則,就不需要引號。
通過打開的 DDE 通道,客戶端應用程序可以使用 DDERequest 函數向服務器應用程序請求文本數據。反之,客戶端可以使用 DDEPoke 語句將數據發(fā)送到服務器應用程序。數據傳送完成后,客戶端可以使用 DDETerminate 語句關閉 DDE 通道,或使用 DDETerminateAll 語句關閉所有打開的通道。
注釋 當客戶端應用程序完成通過 DDE 通道接收數據后,應關閉該通道以節(jié)省內存資源。
下面的示例將演示如何用 Visual Basic 創(chuàng)建 Microsoft Word 過程,并將 Microsoft Access 用作 DDE 服務器。(必須正在運行 Microsoft Access,才能使該示例工作。)
Sub AccessDDE()
Dim intChan1 As Integer, intChan2 As Integer
Dim strQueryData As String
' Use System topic to open Northwind sample database.
' Database must be open before using other DDE topics.
intChan1 = DDEInitiate("MSAccess", "System")
' You may need to change this path to point to actual location
' of Northwind sample database.
DDEExecute intChan1, "[OpenDatabase C:\Access\Samples\Northwind.mdb]"
' Get all data from Ten Most Expensive Products query.
intChan2 = DDEInitiate("MSAccess", "Northwind.mdb;" _
& "QUERY Ten Most Expensive Products")
strQueryData = DDERequest(intChan2, "All")
DDETerminate intChan2
' Close database.
DDEExecute intChan1, "[CloseDatabase]"
DDETerminate intChan1
' Print retrieved data to Debug Window.
Debug.Print strQueryData
End Sub
以下章節(jié)提供有關 Microsoft Access 支持的有效 DDE 主題方面的信息。
System 主題是一個針對所有基于 Microsoft Windows 應用程序的標準主題。它提供應用程序所支持的其他主題的信息。若要訪問該信息,所用代碼必須先以 "System" 作為 topic 參數調用 DDEInitiate 函數,然后用下面為 item 參數提供的選項之一執(zhí)行 DDERequest 語句。
項目 |
返回 |
SysItems |
Microsoft Access 中 System 主題所支持的項目列表。 |
Formats |
Microsoft Access 可以復制到“剪貼板”上的格式列表。 |
Status |
“Busy”或“Ready”。 |
Topics |
所有已打開的數據庫列表。 |
下面的示例演示帶有 System 主題的 DDEInitiate 和 DDERequest 函數的使用:
' In Visual Basic, initiate DDE conversation with Microsoft Access.
Dim intChan1 As Integer, strResults As String
intChan1 = DDEInitiate("MSAccess", "System")
' Request list of topics supported by System topic.
strResults = DDERequest(intChan1, "SysItems")
' Run OpenDatabase action to open Northwind.mdb.
' You may need to change this path to point to actual location
' of Northwind sample database.
DDEExecute intChan1, "[OpenDatabase C:\Access\Samples\Northwind.mdb]"
database 主題是某個現有數據庫的文件名??梢灾绘I入基本名稱 (Northwind),也可以外加其路徑和 .mdb 擴展名 (C:\Access\Samples\Northwind.mdb)。在開始與該數據庫的 DDE 對話以后,可以請求該數據庫中的對象列表。
注釋 不能使用 DDE 來查詢 Microsoft Access 工作組信息文件。
database 主題支持以下項目。
項目 |
返回 |
TableList |
表列表。 |
QueryList |
查詢列表。 |
FormList |
窗體列表。 |
ReportList |
報表列表。 |
MacroList |
宏列表。 |
ModuleList |
模塊列表。 |
ViewList |
視圖列表。 |
StoredProcedureList |
存儲過程列表。 |
DatabaseDiagramList |
數據庫圖表列表。 |
下面的示例顯示如何從 Visual Basic 過程中打開“羅斯文”示例數據庫里的“雇員”窗體:
' In Visual Basic, initiate DDE conversation with
' Northwind sample database.
' Make sure database is open.
intChan2 = DDEInitiate("MSAccess", "Northwind")
' Request list of forms in Northwind sample database.
strResponse = DDERequest(intChan2, "FormList")
' Run OpenForm action and arguments to open Employees form.
DDEExecute intChan2, "[OpenForm Employees,0,,,1,0]"
這些主題使用以下語法:
databasename; TABLE tablename
databasename; QUERY queryname
databasename; SQL [sqlstring]
部分 |
說明 |
databasename |
表或查詢所在的數據庫或 SQL 語句應用到的數據庫的名稱,后跟一個分號 (;)。數據庫名稱可以只是基本名稱 (Northwind),也可以外加完整路徑和 .mdb 擴展名 (C:\Access\Samples\Northwind.mdb)。 |
tablename |
某個已有表的名稱。 |
queryname |
某個已有查詢的名稱。 |
sqlstring |
一個以分號結尾的有效 SQL 語句,可長達 256 個字符。若要交換的字符多于 256 個,可省略該參數而使用后繼的 DDEPoke 語句來建立 SQL 語句。 例如,以下 Visual Basic 代碼使用 DDEPoke 語句來建立 SQL 語句,然后請求查詢結果。 |
intChan1 = DDEInitiate("MSAccess", "Northwind;SQL") DDEPoke intChan1, "SQLText", "SELECT *" DDEPoke intChan1, "SQLText", " FROM Orders" DDEPoke intChan1, "SQLText", " WHERE [Freight] > 100;" strResponse = DDERequest(intChan1, "NextRow") DDETerminate intChan1 |
下表列出了 TABLE tablename、QUERY queryname 和 SQL sqlstring 主題的有效項目。
項目 |
返回 |
|
All |
表中的所有數據,包括字段名。 |
|
Data |
所有數據行,不含字段名。 |
|
FieldNames |
字段名單行列表。 |
|
FieldNames;T |
字段名(第一行)及其數據類型(第二行)的雙行列表。 |
|
這些是返回的值和它們代表的數據類型: |
||
值 |
數據類型 |
|
0 |
無效 |
|
1 |
True/False(非 Null 值) |
|
2 |
無符號字節(jié) |
|
3 |
2 字節(jié)的帶符號的整型數 (Integer) |
|
4 |
4 字節(jié)的帶符號的整型數 (Long) |
|
5 |
8 字節(jié)的帶符號的整型數 (Currency) |
|
6 |
4 字節(jié)的單精度浮點數 (Single) |
|
7 |
8 字節(jié)的雙精度浮點數 (Double) |
|
8 |
日期/時間 |
|
9 |
二進制數據,最多 256 字節(jié) |
|
10 |
ANSI 文本,不區(qū)分大小寫,最多 256 字節(jié)(文本) |
|
11 |
長二進制(OLE 對象) |
|
12 |
長文本(備注) |
|
NextRow |
表或查詢中下一行的數據。當打開一個通道時,NextRow 返回第一行中的數據。如果當前行是最后一條記錄并運行 NextRow,則請求會失敗。 |
|
PrevRow |
表或查詢中上一行的數據。如果 PrevRow 是新通道中的第一個請求,該表或查詢的最后一行中的數據返回。如果第一條記錄是當前行,則 PrevRow 請求會失敗。 |
|
FirstRow |
表或查詢中第一行的數據。 |
|
LastRow |
表或查詢中最后一行的數據。 |
|
FieldCount |
表或查詢中字段的數目。 |
|
SQLText |
代表表或查詢的 SQL 語句。對表,該項目返回窗體 "SELECT * FROM table;" 中的一個 SQL 語句。 |
|
SQLText;n |
一個 SQL 語句,n 個字符塊的大小,代表該表或查詢,其中 n 是最大為 256 的整數。例如,假定有一個如下 SQL 語句所示的語句: "SELECT * FROM Orders;" 項目 "SQLText;7" 返回以下以 Tab 鍵分隔的塊: "SELECT " "* FROM " "Orders;" |
下面的示例顯示在 Visual Basic 過程中如何使用 DDE 請求“羅斯文”示例數據庫的表中的數據,并將該數據插入到文本文件中:
Sub NorthwindDDE
Dim intChan1 As Integer, intChan2 As Integer, intChan3 As Integer
Dim strResp1 As Variant, strResp2 As Variant, strResp3 As Variant
' In a Visual Basic module, get data from Categories table,
' Catalog query, and Orders table in Northwind.mdb.
' Make sure database is open first.
intChan1 = DDEInitiate("MSAccess", "Northwind;TABLE Shippers")
intChan2 = DDEInitiate("MSAccess", "Northwind;QUERY Catalog")
intChan3 = DDEInitiate("MSAccess", "Northwind;SQL SELECT * " _
& "FROM Orders " _
& "WHERE OrderID > 10050;")
strResp1 = DDERequest(intChan1, "All")
strResp2 = DDERequest(intChan2, "FieldNames;T")
strResp3 = DDERequest(intChan3, "FieldNames;T")
DDETerminate intChan1
DDETerminate intChan2
DDETerminate intChan3
' Insert data into text file.
Open "C:\DATA.TXT" For Append As #1
Print #1, strResp1
Print #1, strResp2
Print #1, strResp3
Close #1
End Sub