会员登录 - 用户注册 - 网站地图 Office中国(office-cn.net),专业Office论坛
当前位置:主页 > 技巧 > Access技巧 > 编程心得绝招 > 经验泛谈 > 正文

将 Microsoft Access 用作 Automation 服务器

时间:2005-08-16 10:34 来源:未知 作者:共享 阅读:
概要
高级:要求具有高级编程、互操作性和多用户技巧。

本文提供并说明有关如何将 Microsoft Access 7.0 或 97 用作 Automation 服务器的信息和示例。使用 Visual Basic for Applications,可以通过支持 Automation 的控制器或客户程序(如 Microsoft Excel、Microsoft Project 或 Microsoft Visual Basic)来操纵 Microsoft Access 的功能。例如,可以在使用 Automation 的 Microsoft Excel 中创建应用程序,打印 Microsoft access 数据库中的报表。

本文假设您熟悉 Visual Basic for Applications,并熟悉如何使用 Microsoft Access 所附带的编程工具来创建 Microsoft Access 应用程序。有关 Visual Basic for Applications 的信息,请参见手册《用 Microsoft access 构建应用程序》。
更多信息
本文讨论以下主题:


●创建对 Microsoft access 的引用
●使用 GetObject() 和 CreateObject() 函数
●了解 UserControl 和 Visible 属性
●查看 Microsoft access 的实例。
●关闭 Microsoft access 的实例
●打开数据库时绕过启动设置
●调用 Microsoft access 函数。
●调用自定义过程
●使用运行时应用程序
●使用安全工作组
●示例:


◎预览或打印报表
◎调用“报表向导”新建报表
创建对 Microsoft access 的引用


Automation 允许使用 Visual Basic 代码通过其它的应用程序控制或处理 Microsoft Access 对象。为使其成为可能,系统将有关 Microsoft Access 对象的信息存放在名为 Msaccess.tlb 的类型库中。如果希望 OLE Automation 代码的性能最佳,可以创建对 Microsoft access 类型库的引用。

备注:本文中的许多示例过程都需要对 Microsoft access 的引用。如果打算使用示例代码,在 Automation 控制器中创建引用一定要遵循下列步骤。

创建引用:


在 Automation 控制器中打开模块。
在“工具”菜单上,单击“引用”。
在“引用”框中,选择 Microsoft Access 8.0 对象库(或 Microsoft access for Windows 95 7.0 版),然后单击“确定”。
使用 Automation 时,不需要创建对 Microsoft access 的引用来处理其对象。不过,创建引用有以下几个优点:


代码运行速度更快。
可以使用“对象浏览器”查看所有 Microsoft access 的对象、属性和方法。这样易于确定每个对象可用的属性和方法。
在 Visual Basic 代码中可以使用 Microsoft Access 常量或直接调用 Microsoft access 函数。
可以将变量声明为应用程序定义的对象类型,例如: Dim objAccess As access.Application
这种声明类型被称为早期绑定,是速度最快的绑定方法。但是,并不是所有的 Automation 控制器都支持这种声明类型。因此,本文中的示例代码使用后期绑定而不是早期绑定。例如,它将变量声明为对象而不是 Access.Application: Dim objaccess As Object
要查明应用程序是支持早期绑定还是后期绑定,请查阅 Automation 控制器文档。
使用 GetObject() 和 CreateObject() 函数


GetObject() 和 CreateObject() 函数允许从 Automation 控制器应用程序中打开或激活 Microsoft Access 的实例并控制其功能。在 Visual Basic 模块中使用这些函数时,激活 Microsoft access 作为 Automation 对象,然后将对象指定给变量。例如,下面是使用 GetObject() 和 CreateObject() 函数的几种方法。
方法 1


通过以下语法,可以使用 GetObject() 函数激活或打开 Microsoft Access 实例和指定的数据库。 Dim objaccess as Object
Set objaccess = GetObject("C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb")
请注意,在代码运行时,根据以下不同条件,objAccess 变量可以引用不同的 Microsoft access 实例:


如果指定的数据库 (Northwind.mdb)是在一个 Microsoft Access 实例中打开的,则 objaccess 引用打开的实例。
如果指定的数据库是在多个 Microsoft Access 实例中打开的,则 objaccess 随机引用其中的某个实例。
如果运行代码时 Microsoft Access 没有运行,则打开新建实例,并且 objaccess 引用该实例。
方法 2


通过下面的语法,可以使用 GetObject() 函数激活 Microsoft Access 实例: Dim objaccess as Object
Set objAccess = GetObject(,"access.Application")
请注意,在代码运行时,根据以下不同条件,objAccess 变量可以引用不同的 Microsoft access 实例:


如果代码运行时 Microsoft Access 实例正在运行,则 objaccess 引用打开的实例。
如果多个 Microsoft Access 实例正在运行,则 objaccess 随机引用其中的某个实例。
如果代码运行时 Microsoft access 没有运行,则产生错误。
方法 3


通过以下语法,可以使用 CreateObject() 函数打开新的 Microsoft Access 实例: Dim objaccess as Object
Set objAccess = CreateObject("access.Application")
objaccess 变量引用新实例的 Application 对象。

备注:作为一种替代方法,某些 Automation 控制器(如 Microsoft Visual Basic 4.0)支持使用关键字“New”来打开 Automation 服务器的新实例,例如: Dim objAccess as New access.Application
请参阅 Automation 控制器文档确定 Automation 控制器是否支持使用“New”关键字。
了解 UserControl 和 Visible 属性


Application 对象的 UserControl 和 Visible 属性对于控制 Microsoft Access 的屏幕显示方式很重要。UserControl 属性使您能确定 Microsoft Access 实例是由用户还是由其它使用 Automation 的应用程序启动的。 Visible 属性使您能确定 Microsoft access 实例是可见的还是被最小化了。

打开或激活 Microsoft Access 实例时,根据在 Visual Basic 中激活 Application 对象时 Microsoft Access 是否已经运行,自动设置 UserControl 和 Visible 属性。例如,下表说明在不同情形下的设置: 激活 MS access 的方法 UserControl Visible
----------------------------------------------------------

打开了 Microsoft access True True
实例之后的 GetObject() 函数

Shell() 函数 True True

尚未打开 Microsoft access 实例时的 False False
GetObject() 函数

CreateObject() 函数 False False

UserControl 属性
--------------------
UserControl 属性


UserControl 属性始终是只读,因此,使用 Automation 无法设置该属性。但是,如果在 Automation 代码空闲时用户对代码进行干预,UserControl 属性可能自动更改。例如,发生以下事件时,UserControl 属性将更改为 False:


用户创建 Microsoft access 实例,该实例将 UserControl 属性设置为 True。
在控制器应用程序中运行 Automation 代码,该代码使用 GetObject() 函数激活以前打开的 Microsoft access 实例。实例使用的对象变量为 Public 或模块级变量。
用户使用 Windows 任务栏(或 Windows NT 中的“任务列表”)还原 Microsoft access。
用户试图通过单击“关闭”框来关闭 Microsoft Access。由于 Automation 控制器有引用该 Microsoft access 实例的 Public 或模块级对象变量,所以实例没有按照设想的方式关闭。而是将实例最小化,这样将 UserControl 和 Visible 属性设置为 False。
同样,如果发生以下事件,会将 UserControl 属性更改为 True。


使用 Automaton 新建 Microsoft access 实例。UserControl 属性为 False。Visible 属性也为 False,因此,实例被最小化。
用户使用 Windows 任务栏(或 Windows NT 中的“任务列表”)还原 Microsoft access。或者,在 Visual Basic 中调用 ShowWindow() API 函数还原使用代码的实例。在上述两种情况下,UserControl 和 Visible 属性将更改为 True。
如果 UserControl 属性为 True,它可以影响控制 Microsoft access 屏幕显示方式的能力。应该特别注意以下限制:


在试图将 Visual 属性设置为 True 时,可能会在 Automation 代码中收到错误消息;UserControl 属性为 True 时 Visible 属性为只读。
无法象 OLE Server 应用程序一样捕获或抑制 Microsoft access 生成的错误消息。如果执行错误的命令(例如,试图打开当前数据库中不存在的窗体),将显示错误消息。
当引用实例的对象变量 (objAccess) 设置为 Nothing 或此变量没有指定范围时,Microsoft access 实例不会自动关闭。
Visible 属性


在不同的条件下,Visible 属性可以为只读或读写。当 UserControl 属性为 True 或 Microsoft Access 实例最初由用户(而不是由 Automation 代码)创建时,Visible 属性为只读。当 UserControl 属性为 False 时,Visible 属性为读写。但是,在少数情况下,如果实例的 Visible 属性已经为 True 并且已经被用户最小化,将 Visible 属性设置为 True 可能无法看见 Microsoft access 实例。
查看 Microsoft access 的实例。


根据激活 Application 对象时是否打开 Microsoft Access,可以通过不同的方式使用 Automation 查看 Microsoft access 的实例。
方法 1


使用 Automation 创建新的 Microsoft Access 实例时,由于 Application 对象的 Visible 属性被自动设置为 False,所以实例处于最小化状态。要显示 Microsoft Access,可以将 Visible 属性设置为 True。例如: Dim objaccess as Object
Set objAccess = CreateObject("access.Application")
objaccess.Visible = True
由于 CreateObject() 函数总是打开指定应用程序的新实例,所以这种方法可以起作用。如果直接在运行 CreateObject() 后设置 Visible 属性,则用户无法通过还原或最小化应用程序窗口重设 UserControl 和 Visible 属性来对实例进行干预。

但是,如果使用 GetObject() 函数而不是 CreateObject() 并设置 Visible 属性,代码在以下情况下可能不起作用:


运行代码时 Microsoft access 已经运行。在此情况下,UserControl 属性被设置为 True,这样 Visible 属性为只读。
使用 Automation 打开 Microsoft access 实例,由用户使用任务栏还原实例,然后由用户最小化实例。
备注:要避开这两个限制,可以使用方法 2 查看 Microsoft access 实例。
方法 2


如果要查看 Microsoft Access 实例而不管用户的干预,可以使用下面所列的 ShowAccess() 过程。此示例函数通过三个 Windows API 调用函数来控制 Microsoft access 实例,不考虑当前其 UserControl 和 Visible 属性的设置如何。 ----------------------------------------------------------------------
DECLARATIONS
----------------------------------------------------------------------

Option Explicit

Declare Function SetForegroundWindow Lib "User32" _
(ByVal hWnd As Long) As Long
Declare Function IsIconic Lib "User32" _
(ByVal hWnd As Long) As Long
Declare Function ShowWindow Lib "User32" _
(ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
Const SW_NORMAL = 1 Show window in normal size
Const SW_MINIMIZE = 2 Show window minimized
Const SW_MAXIMIZE = 3 Show window maximized
Const SW_SHOW = 9 Show window without changing window size

Dim objaccess As Object module-level declaration

----------------------------------------------------------------------
This procedure brings the instance of Microsoft access referred to
as "instance" into view. The instances window size can be SW_NORMAL,
SW_MINIMIZE, SW_MAXIMIZE, or SW_SHOW. If size is omitted, the window is
not changed (SW_SHOW). To call this function, use this syntax:
ShowAccess instance:=objaccess, size:=SW_SHOW
----------------------------------------------------------------------

Sub Showaccess(instance As Object, Optional size As Variant)
Dim hWnd As Long, temp As Long

If IsMissing(size) Then size = SW_SHOW
On Error Resume Next
If Not instance.UserControl Then instance.Visible = True
On Error GoTo 0 turn off error handler
hWnd = instance.hWndaccessApp
temp = SetForegroundWindow(hWnd)
If size = SW_SHOW Then keep current window size
If IsIconic(hWnd) Then temp = ShowWindow(hWnd, SW_SHOW)
Else
If IsIconic(hWnd) And size = SW_MAXIMIZE Then _
temp = ShowWindow(hWnd, SW_NORMAL)
temp = ShowWindow(hWnd, size)
End If
End Sub
方法 3


如果使用自动代码运行时已经运行的 Microsoft Access 实例查看特定的数据库,可以使用 Showaccess() 过程(方法 2 中所述)以及下列示例代码: ----------------------------------------------------------------------
This procedure opens the sample database Northwind.mdb in a new or
existing instance of Microsoft access (if one is already open).

NOTE: This procedure uses the Showaccess() procedure (listed above for
Method 2). You must enter this procedure into the same module as
Showaccess() for the code to run properly.
----------------------------------------------------------------------

Sub OpenNorthwind()
Dim path as String

On Error Resume Next temporary error handling
Set objAccess = GetObject(,"access.Application")

If Err <> 0 Then no existing instances of access
Set objAccess = CreateObject("access.Application")
End If

On Error GoTo OpenNorthwind_ErrHandler normal error handler
ShowAccess instance:=objaccess, size:=SW_MAXIMIZE

With objaccess
path = .SysCmd(Access.acSysCmdaccessDir) & "Samples\Northwind.mdb"
If .DBEngine.Workspaces(0).Databases.Count = 0 Then
.OpenCurrentDatabase filepath:=path
ElseIf LCase(Right(.CurrentDb.Name, Len("northwind.mdb"))) _
<> "northwind.mdb" Then
.CloseCurrentDatabase

.OpenCurrentDatabase filepath:=path
End If
.DoCmd.OpenForm FormName:="Main SwitchBoard"
End With
Exit Sub

OpenNorthwind_ErrHandler:
MsgBox Error$(), , "Open Northwind"
End Sub
关闭 Microsoft access 的实例


通常,当引用实例的对象变量被设置为 Nothing 或在控制器应用程序中没有指定范围时,Microsoft Access 的实例会自动关闭。但是,如果在以下视图中打开任何 Microsoft access 对象,则实例不会象期望的那样自动关闭: 对象 视图
----------------------
Table Datasheet
Design
Query Datasheet
Form Form
Report Print Preview
当 Microsoft Access 对象打开时,直到对象关闭且 Application 对象的 UserControl 属性为 False 时,此实例才关闭。但是,通过使用 Application 对象的“退出”方法可以强行关闭实例。例如,下面的示例代码使用“退出”方法关闭 Microsoft access 的所有实例。 ----------------------------------------------------------------------
DECLARATIONS
----------------------------------------------------------------------

Option Explicit

----------------------------------------------------------------------
This procedure closes all open instances of Microsoft access. Once all
instances are closed, the error handler is run and the procedure ends.
----------------------------------------------------------------------

Sub CloseAllaccess()
Dim objaccess As Object
On Error GoTo CloseAllaccess_ErrHandler
Do
Set objAccess = GetObject(,"access.Application")
objaccess.Quit
Loop
CloseAllaccess_ErrHandler:
Set objaccess = Nothing
End Sub

警告:如果代码调用其它 Microsoft Access 函数,则不得使用“退出”方法关闭通过直接调用 Microsoft Access 函数创建的实例。有关在 Automation 代码中使用 Microsoft Access 函数的详细信息,请参阅本文稍后的“调用 Microsoft access 函数”部分。

备注:通过将对象变量设置为模块级或 public 变量而不是过程级变量,可以防止在其对象变量 (objAccess) 没有指定范围时关闭 Microsoft access 实例。

例如,如果 objAccess 是在过程中声明的,那么它只在过程运行时可用。过程结束后,objAccess 没有指定范围且 Microsoft access 可以自动关闭。

但是,如果 objAccess 是在标准模块的 Declaration 部分中声明的,那么它是模块级变量,可以用于该模块的所有过程。如果 objAccess 在标准模块中是作为 Public 变量声明的,那么它可以在数据库的所有过程中使用。在这两种情况下,Automation 代码空闲时 objAccess 有指定的范围。因此,在使用“退出”方法或关闭 Automation 控制器之前,一直保持与 Microsoft access 的连接。
打开数据库时绕过启动设置


当作为 Automation Server 使用 Microsoft access 时,可以通过不同的方式打开特定的数据库。例如,可以使用 GetObject() 函数并指定数据库。或者,可以使用 Application 对象的 OpenCurrentDatabase 方法打开现有数据库将其作为当前数据库。

如果数据库有自定义“启动”设置或 AutoExec 宏,使用 OpenCurrentDatabase 方法时这些功能是运行的。但是,打开数据库时可以使用 SendKeys 语句模拟按下 SHIFT 键的操作,这将绕过 Startup 设置和 AutoExec 宏。例如,可以使用下面的示例代码: Dim objaccess as object
Set objAccess = CreateObject("access.Application")

ShowAccess instance:=objaccess, size:=SW_MAXIMIZE
SendKeys "+"
Simulates holding down the SHIFT key as the database is being opened
objaccess.OpenCurrentDatabase filepath:= _
"C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb"
为了使 SendKeys 语句正确运行,代码应满足以下条件:


代码应使用后期绑定,这意味着引用 Microsoft Access 实例的变量被声明为对象,例如: Dim objaccess as Object
Set objAccess = CreateObject("access.Application")
不得使用早期绑定,早期绑定使用应用程序定义的对象类型声明变量,例如: Dim objAccess as access.Application
Set objAccess = CreateObject("access.Application")

代码确保 Microsoft Access 处于显示状态,并且鼠标指针位于 Microsoft Access 窗口中。如果 Microsoft Access 不是活动窗口,则运行某个过程,如 ShowAccess() 函数(已经在前面的“查看 Microsoft access 实例”部分给予说明)。
正在用 Automation 控制的数据库将其 AllowBypassKey 属性设置为 True(默认设置)。
OpenCurrentDatabase 方法直接跟在 SendKeys 语句之后,且不包含计算。例如,不使用以下代码: objaccess.OpenCurrentDatabase filepath:= _
objAccess.SysCmd(Access.acSysCmdaccessDir) &"Samples\Northwind.mdb"
SendKeys "+"

而使用下列示例代码: path = objAccess.SysCmd(Access.acSysCmdaccessDir) & _
"Samples\Northwind.mdb"
SendKeys "+"
objaccess.OpenCurrentDatabase filepath:=path

调用 Microsoft access 函数。


在 Automation 代码中,只要在 Application 对象可以看到过程,就可以调用内嵌 Microsoft access 函数,如 Eval()、SysCmd() 或 Dlookup()。要查明函数是否可用,请执行下面的操作:


打开模块。
在“工具”菜单上,单击“引用”。
在“引用”框中,选择 Microsoft Access 8.0 对象库(或 Microsoft access for Windows 95 7.0 版),然后单击“确定”。
在“查看”菜单上,单击“对象浏览器”。
在“对象浏览器”框的“库/数据库”下,选择“Access”(或“Access - Microsoft access for Windows 95” 7.0 版)。
在类(或模块/类 7.0 版)下,单击 Application。注意列出的 Application 对象的函数显示在“成员”框中(在 7.0 版中显示在“方法/属性”框中)。可以在 Automation 代码中使用其中任意一个函数。
在 Automation 代码中使用 Microsoft Access 函数时,可以使用 Application 对象直接调用函数,或者使用设置为 Microsoft access 实例的对象变量间接调用函数。以下详细描述这两种技术。
直接调用 Microsoft access 函数


要直接调用 Microsoft Access 函数,首先必须在 Automation 控制器中有对 Microsoft Access 8.0 对象库(或 Microsoft Access for Windows 95 7.0 版)的引用。有关创建引用的详细信息,请参阅本文的“创建 Microsoft access 的引用”部分。

创建对 Microsoft Access 的引用后,可以使用“Access”Application 对象调用 Microsoft Access 函数,例如: MsgBox access.Eval("2+2") displays "4"
MsgBox Access.SysCmd(Access.acSysCmdaccessDir) displays the path
Automation 控制器首次直接调用 Microsoft Access 函数时,创建新的最小化的 Microsoft Access 实例。控制器保持与该实例之间的连接,以防代码调用其它的 Microsoft access 函数。直到关闭控制器应用程序之前,该连接一直保持有效。

备注:不得使用 Application 对象的“退出”方法关闭通过直接调用 Microsoft Access 函数创建的实例。这会在后来调用 Microsoft access 函数时在控制器中导致 Automation 错误。为避免可能的错误,可以在关闭控制器时让控制器自动关闭最小化的实例。
间接调用 Microsoft access 函数


要间接调用 Microsoft Access 函数,不需要创建对象库的引用(与直接调用时的情况不同)。在 Automation 代码中,可以使用为 Microsoft Access 实例设置的对象变量来调用 Microsoft Access 函数,例如: Dim objaccess as Object
On Error Resume Next
Set objAccess = GetObject(,"access.Application")
If Err <> 0 Then no instance of access is open
Set objAccess = CreateObject("access.Application")
End If
MsgBox objaccess.Eval("2+2") displays 4
MsgBox objAccess.SysCmd(Access.acSysCmdaccessDir) displays the path
调用自定义过程


在 Automation 代码中,通过使用 Application 对象的“运行”方法调用存放在 Microsoft access 数据库中的自定义 Visual Basic 过程。自定义过程必须作为 Public 声明并且要位于标准模块(非窗体或报表模块)中。例如,可以向 Northwind.mdb 示例数据库的新模块中添加下面的函数: Public Function MyDateAdd(interval As String, number As Integer, _
startdate As Date) As Date
MyDateAdd = DateAdd(interval, number, startdate)
Calls the Microsoft access built-in DateAdd function.
End Function
要运行上述函数,请将下面的示例代码添加到 Automation 控制器中。 Dim objaccess as Object, newdate as Date
Set objaccess = GetObject _
("C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb")
newdate = objaccess.Run("MyDateAdd", "m", 1, Date)
MsgBox newdate,,"MyDateAdd returned"
使用运行时应用程序


如果要使用 Automation 控制运行时 Microsoft Access 应用程序,需要对代码进行一些更改,特别是用户计算机上没有安装完整的 Microsoft access 零售版时:


如果没有数据库,Microsoft Access 运行时版本就无法启动,因此首先应该验证实例是否已经运行。如果没有运行,应使用 Shell() 函数打开运行时版本并指定 Msaccess.exe 和数据库的路径。
代码启动 Microsoft access 运行时实例后,使用 GetObject()to 引用此实例。
确保在打开数据库前代码没有显示运行时实例。否则,实例将在屏幕上短暂显示,然后变为最小化。
如果要关闭 Microsoft Access 的运行时实例,请使用 Application 对象的“退出”方法,例如: objaccess.Quit

如果要打开 Microsoft access 运行时实例,可以使用下面的示例过程: ----------------------------------------------------------------------
DECLARATIONS
----------------------------------------------------------------------

Option Explicit
Dim objaccess as Object

----------------------------------------------------------------------
This procedure sets a module-level variable, objaccess, to refer to
an instance of Microsoft access. The code first tries to use GetObject
to refer to an instance that might already be open and contains the
specified database (dbpath). If the database is not already open in
an instance of Microsoft access, a new instance of the full version of
Microsoft Access is opened. If the full version of Microsoft access is
not installed, the Shell() function starts a run-time instance of
Microsoft access. Once the instance is opened, you can use the
CloseCurrentDatabase and OpenCurrentDatabase methods to work with other
databases.
----------------------------------------------------------------------

Sub OpenRunTime()
Dim accpath As String, dbpath As String
On Error Resume Next
dbpath = "C:\My Application\MyApp.mdb"
Set objaccess = GetObject(dbpath)
If Err <> 0 Then
If Dir(dbpath) = "" Then dbpath is not valid
MsgBox "Couldnt find database."
Exit Sub
Else The full version of Microsoft access is not installed.
accpath = "C:\Program Files\Common Files\Microsoft Shared" & _
"\Microsoft Access Runtime\MSaccess.exe"
If Dir(accpath) = "" Then
MsgBox "Couldnt find Microsoft access."
Exit Sub
Else
Shell pathname:=accpath & " " & Chr(34) & dbpath & Chr(34), _
windowstyle:=6
Do Wait for shelled process to finish
Err = 0
Set objaccess = GetObject(dbpath)
Loop While Err <> 0
End If
End If
End If
End Sub
使用安全工作组


如果要控制的 Microsoft Access 应用程序使用了安全工作组 (System.mdw),则可能想绕过要求提供用户名和密码的登录框。下面的示例代码使用 Shell() 函数启动 Microsoft access 并将用户名和密码传给应用程序: ----------------------------------------------------------------------
DECLARATIONS
----------------------------------------------------------------------

Option Explicit
Dim objaccess as Object

----------------------------------------------------------------------
This procedure sets a module-level variable, objaccess, to refer to
an instance of Microsoft access. The code first tries to use GetObject
to refer to an instance that might already be open. If an instance is
not already open, the Shell() function opens a new instance and
specifies the user and password, based on the arguments passed to the
procedure.

Calling example: OpenSecured varUser:="Admin", varPw:=""
----------------------------------------------------------------------

Sub OpenSecured(Optional varUser As Variant, Optional varPw As Variant)
Dim cmd As String
On Error Resume Next
Set objAccess = GetObject(, "access.Application")
If Err <> 0 Then no instance of access is open
If IsMissing(varUser) Then varUser = "Admin"
cmd = "C:\Program Files\Microsoft Office\Office\MSaccess.exe"
cmd = cmd & " /nostartup /user " & varUser
If Not IsMissing(varPw) Then cmd = cmd & " /pwd " & varPw
Shell pathname:=cmd, windowstyle:=6
Do Wait for shelled process to finish.
Err = 0
Set objAccess = GetObject(, "access.Application")
Loop While Err <> 0
End If
End Sub
示例


本部分包含通过 Automation 控制器(如 Microsoft Excel、Microsoft Project 或 Microsoft Visual Basic)控制 Microsoft access 功能的两个示例程序。这两个程序执行以下任务: 预览或打印报表,调用“报表向导”创建新报表。

备注:在 Automation 控制器应用程序中这些示例程序需要对 Microsoft Access 对象库的引用。有关创建引用的详细信息,请参阅本篇中的“创建 Microsoft access 的引用”
预览或打印报表


可以使用下面的示例程序,通过 Automation 控制器打印或预览 Microsoft access 中的报表: ----------------------------------------------------------------------
DECLARATIONS
----------------------------------------------------------------------

Option Explicit

----------------------------------------------------------------------
This procedure prints or previews a report, and then closes the current
instance of Microsoft Access (because objaccess is a procedure-level
variable). To call this procedure, use the following syntax:
PrintaccessReport _
dbname:= _
"C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb",
rptname:="Sales by Category", preview:=True
----------------------------------------------------------------------
Sub PrintaccessReport(dbname As String, rptname As String, _
preview As Boolean)
Dim objaccess As Object
On Error GoTo PrintaccessReport_ErrHandler
Set objAccess = CreateObject("access.Application")
With objaccess
.OpenCurrentDatabase filepath:=dbname
If preview Then Preview report on screen.
.Visible = True
.DoCmd.OpenReport reportname:=rptname, _
view:=access.acPreview
Else Print report to printer.
.DoCmd.OpenReport reportname:=rptname, _
view:=access.acNormal
DoEvents Allow report to be sent to printer.
End If
End With
Set objaccess = Nothing
Exit Sub

PrintaccessReport_ErrHandler:
MsgBox Error$(), , "Print access Report"
End Sub
调用“报表向导”新建报表


可以使用下面的示例程序从 Automation 控制器中打开 Microsoft access 中的“报表向导”: ----------------------------------------------------------------------
DECLARATIONS
----------------------------------------------------------------------

Option Explicit
Dim objaccess as Object

----------------------------------------------------------------------
This procedure starts the Report Wizard in Microsoft access using a
specified database and table (or query) as the record source. This
procedure does not close the instance of Microsoft access because
objaccess is a module-level variable. To call this procedure, use the
following syntax:
CallReportWizard _
dbname:= _
"C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb",
sourcetype:="table", sourcename:="Employees"
----------------------------------------------------------------------

Sub CallReportWizard(dbname As String, sourcetype As String, _
sourcename As String)
Dim objtype As Integer
On Error GoTo CallReportWizard_ErrHandler
Set objAccess = CreateObject("access.Application")
With objaccess
.Visible = True
.OpenCurrentDatabase filepath:=dbname
If LCase(sourcetype) = "table" Then
objtype = access.acTable
Else
objtype = access.acQuery
End If
.DoCmd.SelectObject objecttype:=objtype, _
objectname:=sourcename, inDatabaseWindow:=True

Although the following line of code works in Microsoft access 97,
DoMenuItem exists only for backward compatibility. In Microsoft
access 97, you should use the following RunCommand method instead:
.DoCmd.RunCommand (acCmdNewObjectReport)
.DoCmd.DoMenuItem MenuBar:=1, MenuName:=3, Command:=3, _
Version:=access.acMenuVer70
Database menubar, Insert menu, Report command
End With
Exit Sub

CallReportWizard_ErrHandler:
If Err <> 2501 Then Error did not occur by canceling Report Wizard.
MsgBox Error$(), , "Call Report Wizard"
End If
End Sub

(责任编辑:admin)

顶一下
(0)
0%
踩一下
(0)
0%
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价: