将 Microsoft Access 用作 DDE 服务器

expandtri全部显示

Microsoft Access 以目标(客户端)应用程序或源(服务器)应用程序形式支持动态数据交换 (DDE)。例如,一个作为客户端的应用程序,如 Microsoft Word,可以通过 DDE 从一个作为服务器的 Microsoft Access 数据库请求数据。

blueup提示

客户端和服务器之间的 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 语句将命令从客户端发送到服务器应用程序。在用作 DDE 服务器时,Microsoft Access 将以下命令均识别为有效命令:

?当前数据库中的宏名称。
?在 Visual Basic 中,通过使用 DoCmd 对象的某个方法可以执行的任何操作。
?只为 DDE 运算而使用的 OpenDatabase 和 CloseDatabase 操作(有关如何使用这些操作的信息,请参阅该主题以后的示例)。

注释  当将一个操作指定为 DDEExecute 语句时,该操作和所有参数均遵循 DoCmd 对象的语法且必须包含在方括号([ ])中。但支持 DDE 的应用程序并不识别 DDE 运算中的固有常量。同样,如果字符串包含逗号,则必须将该字符串参数包括在引号 (" ")中。否则,就不需要引号。

通过打开的 DDE 通道,客户端应用程序可以使用 DDERequest 函数向服务器应用程序请求文本数据。反之,客户端可以使用 DDEPoke 语句将数据发送到服务器应用程序。数据传送完成后,客户端可以使用 DDETerminate 语句关闭 DDE 通道,或使用 DDETerminateAll 语句关闭所有打开的通道。

注释  当客户端应用程序完成通过 DDE 通道接收数据后,应关闭该通道以节省内存资源。

下面的示例将演示如何用 Visual Basic 创建 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

以下章节提供有关 Microsoft Access 支持的有效 DDE 主题方面的信息。

System 主题

System 主题是一个针对所有基于 Microsoft Windows 应用程序的标准主题。它提供应用程序所支持的其他主题的信息。若要访问该信息,所用代码必须先以 "System" 作为 topic 参数调用 DDEInitiate 函数,然后用下面为 item 参数提供的选项之一执行 DDERequest 语句。

项目

返回

SysItems

Microsoft Access 中 System 主题所支持的项目列表。

Formats

Microsoft Access 可以复制到“剪贴板”上的格式列表。

Status

“Busy”或“Ready”。

Topics

所有已打开的数据库列表。

 

下面的示例演示带有 System 主题的 DDEInitiateDDERequest 函数的使用:

' 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 主题是某个现有数据库的文件名。可以只键入基本名称 (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]"

TABLE tablename、QUERY queryname 和 SQL sqlstring 主题

这些主题使用以下语法:

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

无符号字节


3

2 字节的带符号的整型数 (Integer)


4

4 字节的带符号的整型数 (Long)


5

8 字节的带符号的整型数 (Currency)


6

4 字节的单精度浮点数 (Single)


7

8 字节的双精度浮点数 (Double)


8

日期/时间


9

二进制数据,最多 256 字节


10

ANSI 文本,不区分大小写,最多 256 字节(文本)


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