【问题标题】:Opening an MS-Access database from the command line without running any of the startup vba code?从命令行打开 MS-Access 数据库而不运行任何启动 vba 代码?
【发布时间】:2013-05-20 19:52:28
【问题描述】:

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

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

我正在使用以下code 在单独的 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

这条线...
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 ...在命令行中。

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

【问题讨论】:

  • 您能否调整您尝试打开的数据库中的 AutoExec 宏?也许让它在“非交互式”打开它之前查找您可以设置的注册表值或环境变量,如果该值存在则中止 AutoExec 宏?
  • @GordThompson 你的意思是这样吗? office.microsoft.com/en-us/access-help/…
  • 类似的东西,是的。我并没有特别考虑“访问配置文件”,只是您的代码在使用Shell() 启动访问之前可以创建的一些值。 Access 中的 AutoExec 宏可以运行一些 VBA 来检查值以确定它是由人启动的,还是由您的代码启动的。
  • 考虑/cmd 命令行开关。让 AutoExec 宏检查 Command() 函数返回的值,以查看是否使用了 /cmd。根据 Command() 适当中止 AutoExec。
  • @HansUp 我不想修改我需要通过的每个文件,这会破坏编写脚本的目的。

标签: vba ms-access ms-access-2003


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    Ignore startup options

    【讨论】:

    • OP 询问您是否可以使用命令行打开数据库并绕过 AutoExec。您的答案与从 Windows 资源管理器打开数据库有关,而不是从命令行。
    • @Johnny 是的,此方法在使用命令行时也有效(发布前测试有帮助)。 OP 声明:“我不想修改我需要通过的每个文件。”由于尚未提出使用命令行参数的解决方案,这种方法虽然不是他最初要求的,但完成任务的时间比修改所有文件要少。它一个使用命令行的解决方案,它确实完成了OP所要求的。您的 solution 也不直接涉及命令行,但我没有否决您的答案。
    猜你喜欢
    • 2018-06-24
    • 1970-01-01
    • 2017-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    相关资源
    最近更新 更多