vba 用于打开 Access 数据库、运行宏和持久化 Access 实例的脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20403424/
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
Script to open Access database, run macro, and persist Access instance
提问by enderland
I would like to have a script to:
我想要一个脚本来:
- Open an Access .accdb file
- Run a macro within the database
- Leave this open
- 打开 Access .accdb 文件
- 在数据库中运行宏
- 保持打开状态
I can very easily do the first two with the following VB script:
我可以很容易地使用以下 VB 脚本完成前两个:
dim accessApp
set accessApp = createObject("Access.Application")
accessApp.visible = true
accessApp.OpenCurrentDataBase("C:\path.accdb")
accessApp.Run "myLinker"
But it immediately closes the Access database when the VBS execution finishes. I would like the instance to remain open independent of the script.
但它会在 VBS 执行完成后立即关闭 Access 数据库。我希望实例独立于脚本保持打开状态。
I am not forced to use VBScript for this but it definitely seems the easiest to actually invoke the macro to run.
我没有被迫为此使用 VBScript,但它似乎是最容易实际调用宏来运行的。
回答by Bryan Weaver
If you want to leave the application open after the script completes you need to set the UserControl
property to true
.
如果您想在脚本完成后让应用程序保持打开状态,您需要将该UserControl
属性设置为true
.
dim accessApp
set accessApp = createObject("Access.Application")
accessApp.visible = true
accessApp.UserControl = true
accessApp.OpenCurrentDataBase("C:\path.accdb")
accessApp.Run "myLinker"
The Visible
property is technically unnecessary when the UserControl
property is true. It will automatically be set.
Visible
当UserControl
属性为真时,该属性在技术上是不必要的。它会自动设置。
More information here: http://msdn.microsoft.com/en-us/library/office/ff836033.aspx
更多信息在这里:http: //msdn.microsoft.com/en-us/library/office/ff836033.aspx
回答by engineersmnky
You could also just use a .bat
or .cmd
file and put this because MSACCESS has a command line switch for running a macro and unless that macro closes the database it will remain open for user control.
您也可以只使用一个.bat
或.cmd
文件并放置它,因为 MSACCESS 有一个用于运行宏的命令行开关,除非该宏关闭数据库,否则它将保持打开状态以供用户控制。
START "" /MAX "PATH\TO\MSACCESS.EXE" "C:\path.accdb" /x myLinker
START "" /MAX "PATH\TO\MSACCESS.EXE" "C:\path.accdb" /x myLinker