在 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