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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:21:14  来源:igfitidea点击:

Script to open Access database, run macro, and persist Access instance

vbams-accessvbscript

提问by enderland

I would like to have a script to:

我想要一个脚本来:

  1. Open an Access .accdb file
  2. Run a macro within the database
  3. Leave this open
  1. 打开 Access .accdb 文件
  2. 在数据库中运行宏
  3. 保持打开状态

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 UserControlproperty 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 Visibleproperty is technically unnecessary when the UserControlproperty is true. It will automatically be set.

VisibleUserControl属性为真时,该属性在技术上是不必要的。它会自动设置。

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 .bator .cmdfile 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