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

标签 vba ms-access ms-access-2003

有没有办法从命令行打开 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 宏,就无法打开 Access。唯一的解决方案是让 AutoExec 包含确定数据库如何打开的条件参数,并且如果数据库被 shell 化,则不运行命令。这将需要编辑每个数据库以包含此逻辑。

关于vba - 在不运行任何启动 vba 代码的情况下从命令行打开 MS-Access 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16657263/

相关文章:

vba - 使用 VBA 设置图表数据标签格式

mysql - 合并具有单独日期范围的表而不重叠数据 - Access

excel - 从 Access 报告到 Excel 电子表格的某些字段会损坏

excel - MS-Access 2013 无法删除 .laccdb 锁定文件

ms-access - MS Access ADP 断开连接的记录集恢复

excel - 基于多变量的 VBA 案例选择

Excel VBA 宏后期绑定(bind)

vba - 在excel公式中添加$?

ruby 和 accdb(MS Access )