将 Microsoft Access 用作 DDE 服务器
适用于:Access 2013、Office 2013
Microsoft Access 以目标(客户端)应用程序或源(服务器)应用程序形式支持动态数据交换 (DDE)。 例如,一个作为客户端的应用程序,如 Microsoft Word,可以通过 DDE 从一个作为服务器的 Microsoft Access 数据库请求数据。
提示
[!提示] 如果要从另一个应用程序中操纵 Microsoft Access 对象,可能需要考虑使用"自动化"。
客户端和服务器之间的 DDE 对话是建立在特定的主题上的。 主题可以是服务器应用程序所支持格式的数据文件,也可以是提供有关服务器应用程序本身信息的 System 主题。 在特定主题上开始对话后,只能传输与该主题关联的数据项。
例如,假设正在运行 Microsoft Word,并且想要将特定 Microsoft Access 数据库中的数据插入到文档中。 使用 DDEInitiate 函数打开 DDE 通道并将数据库文件名指定为主题,开始与 Microsoft Access 的 DDE 对话。 然后,可以通过该通道将数据从该数据库传输到 word Microsoft。
作为 DDE 服务器,Microsoft Access 支持以下主题:
System 主题
数据库的名称(database 主题)
表的名称(tablename 主题)
查询的名称(queryname 主题)
Microsoft Access SQL 字符串(sqlstring 主题)
建立 DDE 会话后,可以使用 DDEExecute 语句将命令从客户端发送到服务器应用程序。 Microsoft Access 在用作 DDE 服务器时,将以下命令均识别为有效命令:
当前数据库中的宏名称。
可在 Visual Basic 中使用 DoCmd 对象的某个方法执行的任何操作。
OpenDatabase 和 CloseDatabase 操作,仅用于 DDE 操作。 (有关如何使用这些操作的示例,请参阅本主题后面的示例。)
注意
当将一个宏操作指定为 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 的应用程序的标准主题。 它提供有关应用程序支持的其他主题的信息。 若要访问此信息,代码必须首先使用 topic 参数调用 DDEInitiate 函数,然后使用为 item 参数提供的以下其中一个来执行 DDERequest 语句。
项目 |
返回 |
---|---|
SysItems |
Microsoft Access 中 System 主题所支持的项目的列表。 |
Formats |
Microsoft Access 可以复制到“剪贴板”上的格式的列表。 |
状态 |
“Busy”或“Ready”。 |
主题 |
所有已打开的数据库的列表。 |
下面的示例说明如何使用带有 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 主题是某个现有数据库的文件名。 可以只键入基本名称 (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 主题
这些主题使用以下语法:
databasename ; TABLEtablename
databasename ; QUERY查询名称
databasename ; SQL [ sqlstring ]
Part |
说明 |
---|---|
databasename |
表或查询所在的数据库或 SQL 语句应用到的数据库的名称,后跟一个分号 (;)。 数据库名称可以只是基本名称 (Northwind),也可以外加完整路径和 .mdb 扩展名 (C:\Access\Samples\Northwind.mdb)。 |
tablename |
某个已有表的名称。 |
queryname |
某个已有查询的名称。 |
sqlstring |
一个以分号结尾的有效 SQL 语句,可长达 256 个字符。 若要交换的字符多于 256 个,可省略该参数而使用后继的 DDEPoke 语句来建立 SQL 语句。 例如,以下 Visual Basic 代码使用 DDEPoke 语句来建立 SQL 语句,然后请求查询结果。 |
下表列出了 TABLE tablename、QUERY queryname 和 SQL sqlstring 主题的有效项目。
项目 |
返回 |
---|---|
全部 |
表中的所有数据,包括字段名。 |
数据 |
所有数据行,不含字段名。 |
FieldNames |
字段名单行列表。 |
FieldNames;T |
字段名(第一行)及其数据类型(第二行)的双行列表。 以下是返回的值: 值
|
NextRow |
表或查询中下一行的数据。 当打开一个通道时,NextRow 返回第一行中的数据。 如果当前行是最后一条记录并运行 NextRow,则请求会失败。 |
PrevRow |
表或查询中上一行的数据。 如果 PrevRow 是新通道中的第一个请求,该表或查询的最后一行中的数据返回。 如果第一条记录是当前行,则 PrevRow 请求会失败。 |
FirstRow |
表或查询中第一行的数据。 |
LastRow |
表或查询中最后一行的数据。 |
FieldCount |
表或查询中字段的数目。 |
SQLText |
代表表或查询的 SQL 语句。 对于表,此项以“SELECT'*'FROM table;”的形式返回 SQL 语句。 |
SQLText;n |
一个 SQL 语句,n 个字符块的大小,代表该表或查询,其中 n 是最大为 256 的整数。 例如,假设查询由以下 SQL 语句表示:项“SQLText;7“返回以下制表符分隔的区块:项”SQLText;7“ 返回以下制表符分隔的区块: |
下面的示例显示在 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