2016-07-22 54 views
-2

如何在MS Access的shell語句中運行以下代碼?在VBA中運行批處理代碼以在運行模式下打開特定的Access數據庫文件

"C:\Program Files (x86)\Microsoft Office\Office14\msaccess.exe" "http://stoneplastics/Departments/Quality/Databases/LabelsNewRelease.accdb" /runtime 

它正常工作,在一個批處理文件。 我試了3個小時纔得到這個工作,我有麻煩

+0

看看這個VBA過程:http://stackoverflow.com/questions/37822050/cant-run-dir-from-wscript-shell-in-vba – Tim

+0

我想通了。我確實檢查了它,但沒有奏效。 –

回答

2

因爲要根據接入會話中運行VBA代碼,你可以使用SysCmd(acSysCmdAccessDir)來確定MSACCESS.EXE所在的文件夾。這意味着您不必爲多個Access版本和32位與64位路徑硬編碼文件路徑,然後使用Dir()來檢查MSACCESS.EXE是否存在那裏。

這裏是從即時窗口會工作的例子在Access 2010:

strExe = SysCmd(acSysCmdAccessDir) & "MSACCESS.EXE" 
strDb = "C:\share\Access\Database2.accdb" 

strCommandLine = """" & strExe & """ """ & strDb & """ /runtime" 
Debug.Print strCommandLine 
"C:\Program Files (x86)\Microsoft Office\Office14\MSACCESS.EXE" "C:\share\Access\Database2.accdb" /runtime 

Shell strCommandLine, vbMaximizedFocus 

當你適應,作爲一個VBA程序,它會比你現在有什麼簡單。而且它更靈活,因爲您無需指定在哪裏可以找到MSACCESS.EXE;這是在運行時自動發現的。

+1

我用你的代碼,它像一個時鐘。這不僅對新版本的訪問打擊市場變化的目錄時有好處,而且還改善了性能。非常感謝你 :) –

0

我想通了。這是我使用的代碼。 (請注意,我改變了Access數據庫文件。

If Not Len(Dir("C:\Program Files\Microsoft Office\Office12\msaccess.exe", vbDirectory)) = 0 Then 
Call Shell(Chr$(34) & "C:\Program Files\Microsoft Office\Office12\msaccess.exe" & Chr$(34) & " " & Chr$(34) & "http://stoneplastics/Departments/Quality/Databases/ItemInspectionInstructionPrintOutRev3.accdb" & Chr$(34) & " /runtime", vbMaximizedFocus) 
End If 
If Not Len(Dir("C:\Program Files\Microsoft Office\Office14\msaccess.exe", vbDirectory)) = 0 Then 
Call Shell(Chr$(34) & "C:\Program Files\Microsoft Office\Office14\msaccess.exe" & Chr$(34) & " " & Chr$(34) & "http://stoneplastics/Departments/Quality/Databases/ItemInspectionInstructionPrintOutRev3.accdb" & Chr$(34) & " /runtime", vbMaximizedFocus) 
End If 
If Not Len(Dir("C:\Program Files\Microsoft Office\Office11\msaccess.exe", vbDirectory)) = 0 Then 
Call Shell(Chr$(34) & "C:\Program Files\Microsoft Office\Office11\msaccess.exe" & Chr$(34) & " " & Chr$(34) & "http://stoneplastics/Departments/Quality/Databases/ItemInspectionInstructionPrintOutRev3.accdb" & Chr$(34) & " /runtime", vbMaximizedFocus) 
End If 
If Not Len(Dir("C:\Program Files\Microsoft Office\Office15\msaccess.exe", vbDirectory)) = 0 Then 
Call Shell(Chr$(34) & "C:\Program Files\Microsoft Office\Office15\msaccess.exe" & Chr$(34) & " " & Chr$(34) & "http://stoneplastics/Departments/Quality/Databases/ItemInspectionInstructionPrintOutRev3.accdb" & Chr$(34) & " /runtime", vbMaximizedFocus) 
End If 
If Not Len(Dir("C:\Program Files (x86)\Microsoft Office\Office14\msaccess.exe", vbDirectory)) = 0 Then 
Call Shell(Chr$(34) & "C:\Program Files (x86)\Microsoft Office\Office14\msaccess.exe" & Chr$(34) & " " & Chr$(34) & "http://stoneplastics/Departments/Quality/Databases/ItemInspectionInstructionPrintOutRev3.accdb" & Chr$(34) & " /runtime", vbMaximizedFocus) 
End If 
If Not Len(Dir("C:\Program Files (x86)\Microsoft Office\Office12\msaccess.exe", vbDirectory)) = 0 Then 
Call Shell(Chr$(34) & "C:\Program Files (x86)\Microsoft Office\Office12\msaccess.exe" & Chr$(34) & " " & Chr$(34) & "http://stoneplastics/Departments/Quality/Databases/ItemInspectionInstructionPrintOutRev3.accdb" & Chr$(34) & " /runtime", vbMaximizedFocus) 
End If 
If Not Len(Dir("C:\Program Files (x86)\Microsoft Office\Office15\msaccess.exe", vbDirectory)) = 0 Then 
Call Shell(Chr$(34) & "C:\Program Files (x86)\Microsoft Office\Office15\msaccess.exe" & Chr$(34) & " " & Chr$(34) & "http://stoneplastics/Departments/Quality/Databases/ItemInspectionInstructionPrintOutRev3.accdb" & Chr$(34) & " /runtime", vbMaximizedFocus) 
End If 
If Not Len(Dir("C:\Program Files (x86)\Microsoft Office\Office11\msaccess.exe", vbDirectory)) = 0 Then 
Call Shell(Chr$(34) & "C:\Program Files (x86)\Microsoft Office\Office11\msaccess.exe" & Chr$(34) & " " & Chr$(34) & "http://stoneplastics/Departments/Quality/Databases/ItemInspectionInstructionPrintOutRev3.accdb" & Chr$(34) & " /runtime", vbMaximizedFocus) 
End If 
If Not Len(Dir("C:\Program Files\Microsoft Office 15\root\office15\msaccess.exe", vbDirectory)) = 0 Then 
Call Shell(Chr$(34) & "C:\Program Files\Microsoft Office 15\root\office15\msaccess.exe" & Chr$(34) & " " & Chr$(34) & "http://stoneplastics/Departments/Quality/Databases/ItemInspectionInstructionPrintOutRev3.accdb" & Chr$(34) & " /runtime", vbMaximizedFocus) 
End If 
+0

VBA代碼是否會在Access會話中運行? – HansUp

+0

是的。它將運行在一個私人分 –

0

不知道你是如何計算「它不工作」當你將命令傳遞到外殼像dbmitch的回答後,並像dbmitch在演示用引號括起來這個帖子,但很高興你得到它的工作。:)

另外,因爲定位每一個可能的路徑訪問顯然是一個要求,爲什麼不直接使用迪爾找到它呢?由於Office16(32位)路徑爲「\ Program Files(x86)\ Microsoft Office \ root \ Office16 \ MSACCESS.EXE」,Office 17可能會有所不同,這將有助於防止錯誤並驗證您的代碼。如果你創建一個Shell對象並調用.Exec方法,你可以得到Command的DIR函數的輸出(不是VBA的DIR函數...... 2個非常不同的野獸!)。這將是訪問的路徑。這還可以防止用戶將Access/Office安裝在默認位置以外的任何錯誤。

Dim oShell As Object 'New WshShell 
Dim cmd As Object 'WshExec 
Dim strPath as String 'Path to Access 
Const WshRunning = 0 

On Error GoTo wshError 

Set oShell = CreateObject("Wscript.Shell") 
'Note the two double quotes "". This causes VBA to insert one double quote. 
'Its a little cleaner than " & Chr$(34) & " 
'http://stackoverflow.com/questions/9024724/how-do-i-put-double-quotes-in-a-string-in-vba/38456820 
Set cmd = oShell.Exec("cmd /c ""dir c:\*MSACCESS.EXE /a:-d /b /d /s""") 
Do While cmd.Status = WshRunning 
    'Do nothing 
    DoEvents 
Loop 

strPath = cmd.StdOut.ReadAll 
If strPath="" Then 
    MsgBox "Access was not found." 
    Exit Sub 
End If 

'We now have the path to Access so pass it to shell like you've done 
Call Shell("""" & strPath & """" & _ 
    " ""http://stoneplastics/Departments/Quality/Databases/ItemInspectionInstructionPrintOutRev3.accdb"" /runtime" _ 
    , vbMaximizedFocus) 

Set oShell = Nothing 
Set cmd = Nothing 
Exit sub 

wshError: 
On Error Resume Next 
Msgbox cmd.StdErr.ReadAll 
Resume Next 
+0

我更喜歡你的答案。我認爲,當涉及到dbmitch的解決方案時,我無法獲得適當的語法。當我們升級時,我會給解決方案一個:) –