Table of contents

Application.CloseCurrentDatabase 方法 (访问)Application.CloseCurrentDatabase Method (Access)

office 365 dev account|上次更新日期: 2018/3/13
2 参与人员

您可以使用CloseCurrentDatabase方法关闭当前的数据库 (Microsoft Access 数据库或 Access 项目 (.adp) 从另一个应用程序已打开数据库通过自动化。You can use the CloseCurrentDatabase method to close the current database (either a Microsoft Access database or an Access project (.adp) from another application that has opened a database through Automation.


表达式CloseCurrentDatabaseexpression. CloseCurrentDatabase

表达式_一个表示应用程序对象的变量。_expression A variable that represents an Application object.

返回值Return Value



例如,可以从 Microsoft Excel 中使用该方法在打开另一个数据库之前关闭当前在 Microsoft Access 窗口中打开的数据库。For example, you might use this method from Microsoft Excel to close the database currently open in the Microsoft Access window before opening another database.

从另一个应用程序通过自动化打开 Microsoft Access 数据库时, CloseCurrentDatabase方法很有用。从其他应用程序创建的 Microsoft Access 实例之后, 还必须创建一个新数据库或指定要打开的现有数据库。在 Microsoft Access 窗口中打开此数据库。The CloseCurrentDatabase method is useful when you have opened a Microsoft Access database from another application through Automation. Once you have created an instance of Microsoft Access from another application, you must also create a new database or specify an existing database to open. This database opens in the Microsoft Access window.

如果您使用CloseCurrentDatabase方法关闭当前的 Microsoft Access 实例中打开该数据库,然后可以而无需创建 Microsoft Access 实例中的打开一个不同的数据库。If you use the CloseCurrentDatabase method to close the database that is open in the current instance of Microsoft Access, you can then open a different database without having to create another instance of Microsoft Access.


以下示例通过 Automation 从其他应用程序打开一个 Microsoft Access 数据库,在其中创建一个新窗体并保存它,然后关闭该数据库。The following example opens a Microsoft Access database from another application through Automation, creates a new form and saves it, then closes the database.

可以将这段代码添加到任何能够作为 COM 组件的应用程序的 Visual Basic 模块中。例如,可从 Microsoft Excel 或 Microsoft Visual Basic 中运行下面的代码。You can enter this code in a Visual Basic module in any application that can act as a COM component. For example, you might run the following code from Microsoft Excel or Microsoft Visual Basic.

当指向应用程序对象变量超出范围时,Microsoft Access 所表示的实例也关闭。因此,应声明该变量在模块级别。When the variable pointing to the Application object goes out of scope, the instance of Microsoft Access that it represents closes as well. Therefore, you should declare this variable at the module level.

' Enter following in Declarations section of module. 
Dim appAccess As Access.Application 

Sub CreateForm() 
 Const strConPathToSamples = "C:\Program Files\Microsoft Office\Office12\Samples\" 

 Dim frm As Form, strDB As String 

 ' Initialize string to database path. 
 strDB = strConPathToSamples &; "Northwind.mdb" 
 ' Create new instance of Microsoft Access. 
 Set appAccess = CreateObject("Access.Application") 
 ' Open database in Microsoft Access window. 
 appAccess.OpenCurrentDatabase strDB 
 ' Create new form. 
 Set frm = appAccess.CreateForm 
 ' Save new form. 
 appAccess.DoCmd.Save , "NewForm1" 
 ' Close currently open database. 
 Set AppAccess = Nothing 
End Sub

另请参阅See also


Application 对象Application Object

© 2018 Microsoft