使用来自单独数据库的 VBA 代码关闭数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11313590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:43:15  来源:igfitidea点击:

Closing a database using VBA code from a seperate database

databasems-accessvbams-access-2007

提问by Sinister Beard

Is it possible to close one Access database (let's call it Database A) via VBA code from another Database (Database B).

是否可以通过来自另一个数据库(数据库 B)的 VBA 代码关闭一个 Access 数据库(我们称之为数据库 A)。

In the example here, if Database A is open when Database B launches, I would want Database A to close. Is this possible using VBA?

在这里的示例中,如果数据库 A 在数据库 B 启动时打开,我希望数据库 A 关闭。这可以使用VBA吗?

I've had a google about, but all answers seem to be related to closing the current database using VBA, which of course I could achieve with DoCmd.Quit.

我有一个关于谷歌的搜索,但所有的答案似乎都与使用 VBA 关闭当前数据库有关,当然我可以使用 DoCmd.Quit 来实现。

Any help is greatly appreciated.

任何帮助是极大的赞赏。

回答by Fionnuala

You can hiHyman it:

你可以劫持它:

Dim OtherDB As Object

sOther = "Z:\Documents\other.accdb"
Set OtherDB = GetObject(sOther)
OtherDB.Application.Quit

It may make life difficult for someone.

这可能会使某人的生活变得困难。

回答by kristine

I know this question is old, but maybe this will help someone. I had a database in which I used VBA to open another db and run a macro. I wanted to then close the db. The code which worked for me is below, it includes the entire process from open to close.

我知道这个问题很老,但也许这会对某人有所帮助。我有一个数据库,在其中使用 VBA 打开另一个数据库并运行宏。我想然后关闭数据库。对我有用的代码如下,它包括从打开到关闭的整个过程。

Function functionName()

Static acc As Access.Application
Dim db As DAO.Database
Dim dbname As String
dbname = "absolutePathToDBHere.accdb"
Set acc = New Access.Application
acc.Visible = True
Set db = acc.DBEngine.OpenDatabase(dbname, False, False)
acc.OpenCurrentDatabase dbname
acc.DoCmd.RunMacro ("macroName")
acc.DoCmd.Quit acQuitSaveAll

db.Close
Set db = Nothing

End Function