將 Microsoft Access 用作 DDE 服務(wù)器

此頁沒有內(nèi)容條目
內(nèi)容

expandtri全部顯示

Microsoft Access 以目標(biāo)(客戶端)應(yīng)用程序或源(服務(wù)器)應(yīng)用程序形式支持動態(tài)數(shù)據(jù)交換 (DDE)。例如,一個作為客戶端的應(yīng)用程序,如 Microsoft Word,可以通過 DDE 從一個作為服務(wù)器的 Microsoft Access 數(shù)據(jù)庫請求數(shù)據(jù)。

blueup提示

客戶端和服務(wù)器之間的 DDE 對話是建立在特定的主題上的。主題可以是服務(wù)器應(yīng)用程序所支持格式的數(shù)據(jù)文件,也可以是提供有關(guān)服務(wù)器應(yīng)用程序本身信息的 System 主題。開始了特定主題的對話后,只能傳送與該主題相關(guān)的數(shù)據(jù)項(xiàng)

例如,假定正在運(yùn)行 Microsoft Word,并要將一個特定的 Microsoft Access 數(shù)據(jù)庫中的數(shù)據(jù)插入到文檔中,通過使用 DDEInitiate 函數(shù)將 DDE 通道打開,并將數(shù)據(jù)庫文件名稱指定為主題,即可開始與 Microsoft Access 的 DDE 對話。然后通過該通道即可將數(shù)據(jù)庫中的數(shù)據(jù)傳給 Microsoft Word。

作為 DDE 服務(wù)器,Microsoft Access 支持以下主題:

?System 主題
?數(shù)據(jù)庫名稱 (database 主題)
?表名稱 (tablename 主題)
?查詢名稱 (queryname 主題)
?Microsoft Access SQL 字符串(sqlstring 主題)

建立了 DDE 對話后,可以使用 DDEExecute 語句將命令從客戶端發(fā)送到服務(wù)器應(yīng)用程序。在用作 DDE 服務(wù)器時,Microsoft Access 將以下命令均識別為有效命令:

?當(dāng)前數(shù)據(jù)庫中的宏名稱。
?在 Visual Basic 中,通過使用 DoCmd 對象的某個方法可以執(zhí)行的任何操作。
?只為 DDE 運(yùn)算而使用的 OpenDatabase 和 CloseDatabase 操作(有關(guān)如何使用這些操作的信息,請參閱該主題以后的示例)。

注釋  當(dāng)將一個操作指定為 DDEExecute 語句時,該操作和所有參數(shù)均遵循 DoCmd 對象的語法且必須包含在方括號([ ])中。但支持 DDE 的應(yīng)用程序并不識別 DDE 運(yùn)算中的固有常量。同樣,如果字符串包含逗號,則必須將該字符串參數(shù)包括在引號 (" ")中。否則,就不需要引號。

通過打開的 DDE 通道,客戶端應(yīng)用程序可以使用 DDERequest 函數(shù)向服務(wù)器應(yīng)用程序請求文本數(shù)據(jù)。反之,客戶端可以使用 DDEPoke 語句將數(shù)據(jù)發(fā)送到服務(wù)器應(yīng)用程序。數(shù)據(jù)傳送完成后,客戶端可以使用 DDETerminate 語句關(guān)閉 DDE 通道,或使用 DDETerminateAll 語句關(guān)閉所有打開的通道。

注釋  當(dāng)客戶端應(yīng)用程序完成通過 DDE 通道接收數(shù)據(jù)后,應(yīng)關(guān)閉該通道以節(jié)省內(nèi)存資源。

下面的示例將演示如何用 Visual Basic 創(chuàng)建 Microsoft Word 過程,并將 Microsoft Access 用作 DDE 服務(wù)器。(必須正在運(yùn)行 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é)提供有關(guān) Microsoft Access 支持的有效 DDE 主題方面的信息。

System 主題

System 主題是一個針對所有基于 Microsoft Windows 應(yīng)用程序的標(biāo)準(zhǔn)主題。它提供應(yīng)用程序所支持的其他主題的信息。若要訪問該信息,所用代碼必須先以 "System" 作為 topic 參數(shù)調(diào)用 DDEInitiate 函數(shù),然后用下面為 item 參數(shù)提供的選項(xiàng)之一執(zhí)行 DDERequest 語句。

項(xiàng)目

返回

SysItems

Microsoft Access 中 System 主題所支持的項(xiàng)目列表。

Formats

Microsoft Access 可以復(fù)制到“剪貼板”上的格式列表。

Status

“Busy”或“Ready”。

Topics

所有已打開的數(shù)據(jù)庫列表。

 

下面的示例演示帶有 System 主題的 DDEInitiateDDERequest 函數(shù)的使用:

' 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 主題

database 主題是某個現(xiàn)有數(shù)據(jù)庫的文件名。可以只鍵入基本名稱 (Northwind),也可以外加其路徑和 .mdb 擴(kuò)展名 (C:\Access\Samples\Northwind.mdb)。在開始與該數(shù)據(jù)庫的 DDE 對話以后,可以請求該數(shù)據(jù)庫中的對象列表。

注釋  不能使用 DDE 來查詢 Microsoft Access 工作組信息文件。

database 主題支持以下項(xiàng)目。

項(xiàng)目

返回

TableList

表列表。

QueryList

查詢列表。

FormList

窗體列表。

ReportList

報表列表。

MacroList

宏列表。

ModuleList

模塊列表。

ViewList

視圖列表。

StoredProcedureList

存儲過程列表。

DatabaseDiagramList

數(shù)據(jù)庫圖表列表。

 

下面的示例顯示如何從 Visual Basic 過程中打開“羅斯文”示例數(shù)據(jù)庫里的“雇員”窗體:

' 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]"

TABLE tablename、QUERY queryname 和 SQL sqlstring 主題

這些主題使用以下語法:

databasename; TABLE tablename

databasename; QUERY queryname

databasename; SQL [sqlstring]

部分

說明

databasename

表或查詢所在的數(shù)據(jù)庫或 SQL 語句應(yīng)用到的數(shù)據(jù)庫的名稱,后跟一個分號 (;)。數(shù)據(jù)庫名稱可以只是基本名稱 (Northwind),也可以外加完整路徑和 .mdb 擴(kuò)展名 (C:\Access\Samples\Northwind.mdb)。

tablename

某個已有表的名稱。

queryname

某個已有查詢的名稱。

sqlstring

一個以分號結(jié)尾的有效 SQL 語句,可長達(dá) 256 個字符。若要交換的字符多于 256 個,可省略該參數(shù)而使用后繼的 DDEPoke 語句來建立 SQL 語句。

例如,以下 Visual Basic 代碼使用 DDEPoke 語句來建立 SQL 語句,然后請求查詢結(jié)果。


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 主題的有效項(xiàng)目。

項(xiàng)目

返回

All

表中的所有數(shù)據(jù),包括字段名。

Data

所有數(shù)據(jù)行,不含字段名。

FieldNames

字段名單行列表。

FieldNames;T

字段名(第一行)及其數(shù)據(jù)類型(第二行)的雙行列表。


這些是返回的值和它們代表的數(shù)據(jù)類型:


數(shù)據(jù)類型


0

無效


1

True/False(非 Null 值)


2

無符號字節(jié)


3

2 字節(jié)的帶符號的整型數(shù) (Integer)


4

4 字節(jié)的帶符號的整型數(shù) (Long)


5

8 字節(jié)的帶符號的整型數(shù) (Currency)


6

4 字節(jié)的單精度浮點(diǎn)數(shù) (Single)


7

8 字節(jié)的雙精度浮點(diǎn)數(shù) (Double)


8

日期/時間


9

二進(jìn)制數(shù)據(jù),最多 256 字節(jié)


10

ANSI 文本,不區(qū)分大小寫,最多 256 字節(jié)(文本)


11

長二進(jìn)制(OLE 對象)


12

長文本(備注)

NextRow

表或查詢中下一行的數(shù)據(jù)。當(dāng)打開一個通道時,NextRow 返回第一行中的數(shù)據(jù)。如果當(dāng)前行是最后一條記錄并運(yùn)行 NextRow,則請求會失敗。

PrevRow

表或查詢中上一行的數(shù)據(jù)。如果 PrevRow 是新通道中的第一個請求,該表或查詢的最后一行中的數(shù)據(jù)返回。如果第一條記錄是當(dāng)前行,則 PrevRow 請求會失敗。

FirstRow

表或查詢中第一行的數(shù)據(jù)。

LastRow

表或查詢中最后一行的數(shù)據(jù)。

FieldCount

表或查詢中字段的數(shù)目。

SQLText

代表表或查詢的 SQL 語句。對表,該項(xiàng)目返回窗體 "SELECT * FROM table;" 中的一個 SQL 語句。

SQLText;n

一個 SQL 語句,n 個字符塊的大小,代表該表或查詢,其中 n 是最大為 256 的整數(shù)。例如,假定有一個如下 SQL 語句所示的語句:

"SELECT * FROM Orders;"

項(xiàng)目 "SQLText;7" 返回以下以 Tab 鍵分隔的塊:

"SELECT "

"* FROM "

"Orders;"

 

下面的示例顯示在 Visual Basic 過程中如何使用 DDE 請求“羅斯文”示例數(shù)據(jù)庫的表中的數(shù)據(jù),并將該數(shù)據(jù)插入到文本文件中:

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