用 VBA 打开一个新的数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17342423/
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 21:54:11  来源:igfitidea点击:

Opening a new DB with VBA

vbams-accessms-access-2007

提问by Chuck

I am trying to open another db using VBA from my current db then close the one I am in. When I use the code listed here it opens Access but closes it immediately. I am sure it is simply something I am overlooking but the past hour I have wracked my brain. Any help would be greatly appreciated.

我正在尝试使用当前数据库中的 VBA 打开另一个数据库,然后关闭我所在的数据库。当我使用此处列出的代码时,它会打开 Access 但立即关闭它。我确信这只是我忽略的事情,但过去一个小时我已经伤透了脑筋。任何帮助将不胜感激。

Private Sub Command115_Click()
Dim objAccess As Access.Application

Const conPATH = "C:\Users\user\Desktop\Database1.accdb"

'Create an instance of the Access application object.
Set objAccess = CreateObject("Access.Application")

'Open the database
objAccess.Visible = True
objAccess.OpenCurrentDatabase conPATH

'Open the form.
objAccess.DoCmd.OpenForm "Main-Form"

' Maximize other Access window
objAccess.DoCmd.RunCommand acCmdAppMaximize
End Sub

Thanks in advance for any help in this matter

在此先感谢您对此事的任何帮助

回答by ashareef

If you use the shell then when you close your first database, the second will remain open.

如果您使用 shell,那么当您关闭第一个数据库时,第二个将保持打开状态。

Sub test()
    Dim sh As Variant
    sh = Shell("""C:\...\MSACCESS.EXE"" ""C:\...\FileName.accdb""")
End Sub

My guess the reason your code didn't work is because, the second access you start is an object that exists within the first one. The moment the first one closes and cleanup starts for its objects/variables. It closes the second one.

我猜您的代码不起作用的原因是,您开始的第二次访问是第一个中存在的对象。第一个关闭并开始清理其对象/变量的那一刻。它关闭了第二个。

回答by user2638336

Say your first app is access1 and it is trying to open access2 and then close access1 app and makes access2 as active app. One thing you could do is, Once your access1 app loads try to open access2 app programatically and make it visible and then use Application.closeCurrentdatabase to close access1 database and then in the unload event of access1 apps form (if there is any form in access1 app) call Application.Quit.

假设您的第一个应用程序是 access1,它试图打开 access2,然后关闭 access1 应用程序并使 access2 作为活动应用程序。您可以做的一件事是,一旦您的 access1 应用程序加载尝试以编程方式打开 access2 应用程序并使其可见,然后使用 Application.closeCurrentdatabase 关闭 access1 数据库,然后在 access1 应用程序表单的卸载事件中(如果 access1 中有任何表单) app) 调用 Application.Quit。

This works.

这有效。