在不运行任何启动 vba 代码的情况下从命令行打开 MS-Access 数据库?

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

Opening an MS-Access database from the command line without running any of the startup vba code?

vbams-accessms-access-2003

提问by leeand00

Is there a way to open an MS-Access 2003 database from the command line without running any of the startup vba code or displaying any errors?

有没有办法从命令行打开 MS-Access 2003 数据库而不运行任何启动 vba 代码或显示任何错误?

I looked at the command line arguments for MS Accessand there doesn't seem to be one for specifying that you want none of the vba code to execute on startup.

我查看了 MS Access命令行参数,似乎没有一个参数用于指定您不希望在启动时执行任何 vba 代码。

I'm using the following codeto open up a database in a separate vba database:

我正在使用以下代码在单独的 vba 数据库中打开一个数据库:

Sub test()


Dim accObj As Access.application, Msg As String
Dim application As String, dbs As String, workgroup As String
Dim user As String, password As String, cTries As Integer
Dim x

Dim theDB As Database

' This is the default location of Access
application = "C:\Program Files (x86)\Microsoft Office\OFFICE11\MSACCESS.EXE"

' Use the path and name of a secured MDB on your system
dbs = "C:\ucpdatas\awashic-pc\APLReporting.mdb"

' This is the default working group
workgroup = "E:\Tickets\CSN_NotSure\Secured.mdw"
user = "aleer"
password = "****"

Debug.Print application & " " & Chr(34) & dbs & Chr(34) & " /nostartup /user " & user & " /pwd " & password & " /wrkgrp " & Chr(34) & workgroup & Chr(34), vbMinimizedFocus

x = Shell(application & " " & Chr(34) & dbs & Chr(34) & " /nostartup /user " & user & " /pwd " & password & " /wrkgrp " & Chr(34) & workgroup & Chr(34), vbMinimizedFocus)


On Error GoTo WAITFORACCESS
Set accObj = GetObject(, "Access.Application")

' Turn off error handling
On Error GoTo 0

' You an now use the accObj reference to automate Access
Debug.Print "Access is now open."

' Do Stuff...

accObj.CloseCurrentDatabase
accObj.Quit

' Close it out...
Set accObj = Nothing
Debug.Print "Closed and complete."

Exit Sub

WAITFORACCESS: ' <--- this line must be left-aligned.
' Access isn't registered in the Running Object Table yet, so call
' SetFocus to take focus from Access, wait half a second, and try again.
' If you try five times and fail, then something has probably gone wrong,
' so warn the user and exit.

'SetFocus

If cTries < 5 Then
   cTries = cTries + 1
   Sleep 500 ' wait 1/2 seconds
   Resume
Else
   Debug.Print "It didn't work"
End If

End Sub

This line...
x = Shell(application & " " & Chr(34) & dbs & Chr(34) & " /nostartup /user " & user & " /pwd " & password & " /wrkgrp " & Chr(34) & workgroup & Chr(34), vbMinimizedFocus)
Turns out to be...
C:\Program Files (x86)\Microsoft Office\OFFICE11\MSACCESS.EXE "C:\ucpdatas\awashic-pc\APLReporting.mdb" /nostartup /user aleer /pwd *** /wrkgrp "E:\Tickets\CSN_NotSure\Secured.mdw" 2... at the command line.

这一行...
x = Shell(application & " " & Chr(34) & dbs & Chr(34) & " /nostartup /user " & user & " /pwd " & password & " /wrkgrp " & Chr(34) & workgroup & Chr(34), vbMinimizedFocus)
结果是...
C:\Program Files (x86)\Microsoft Office\OFFICE11\MSACCESS.EXE "C:\ucpdatas\awashic-pc\APLReporting.mdb" /nostartup /user aleer /pwd *** /wrkgrp "E:\Tickets\CSN_NotSure\Secured.mdw" 2...在命令行中。

But when the database opens it executes a bunch of vba codes and displays error messages.

但是当数据库打开时,它会执行一堆 vba 代码并显示错误消息。

采纳答案by Johnny Bones

There is no way for Access to open without running the AutoExec macro associated with that database. The only solution would be to have the AutoExec contain conditional arguments that determined how the database was opened, and not run the commands if the database was shell'd. This would require editing every database to include this logic.

如果不运行与该数据库关联的 AutoExec 宏,就无法打开 Access。唯一的解决方案是让 AutoExec 包含确定数据库如何打开的条件参数,并且如果数据库被 shell 化,则不运行命令。这将需要编辑每个数据库以包含此逻辑。

回答by GlennFromIowa

Technically, yes there is a way to open an MS-Access 2003 database from the command line without running any of the startup macros, although it does not involve the command line arguments: If you hold down the Shift key while the database opens, it will not run the AutoExec script (and suppresses a few other things). This also assumes the AllowBypassKey property has not been set to False.

从技术上讲,是的,有一种方法可以在不运行任何启动宏的情况下从命令行打开 MS-Access 2003 数据库,尽管它不涉及命令行参数:如果在数据库打开时按住 Shift 键,它将不会运行 AutoExec 脚本(并抑制其他一些事情)。这还假定 AllowBypassKey 属性尚未设置为 False。

See Ignore startup options

请参阅忽略启动选项