2016-02-04 41 views
0

我有一個訪問數據庫,我想從其他用戶的共享驅動器中完全運行,並將VBA寫入FrontEnd表單。使用VBA從網絡調用批處理文件

作爲其中一個函數的一部分,它使用VBA調用批處理文件將其他文件壓縮爲文本文件,然後將其鏈接並導入到數據庫中。

但是,由於將數據庫移動到共享驅動器以及批處理文件,因此無法找到批處理文件,因爲CMD稱UNC路徑不受支持。另外理想情況下,我希望VBA在繼續使用VBA之前等待批處理文件完成,因爲我目前有另一個使代碼等待一段時間的函數(WaitFor(10))。

CMD聲明「U:\ My」不被識別爲內部或外部命令。這是VBA編碼還是批處理文件?

碼關注:

Dim txtPath, batpath, FormDate, tempPath As String 

FormDate = Format(Now(), "yyyymmdd") 
batpath = Application.CurrentProject.Path & "\Compressor\" 
txtPath = Application.CurrentProject.Path & "\Data_TextFile\" 
tempPath = Application.CurrentProject.Path & "\TempTextFlows\" 

'Do files exist in temp folder? 
If Dir(tempPath & "*.*") = "" Then 

    'If files don't exist change status to: 
    Me.Text41 = "No File(s) Found" 
    Else 

    'If files do exist change status to: 
    Me.Text41 = "Compressing Files, Please Wait..." 

    'Call the Files compressor 
    Call Shell(Environ$("COMSPEC") & " /c " & batpath & "myBat.bat", vbNormalFocus) 

    'Wait 5 seconds for flow compressor to run 
    Call WaitFor(10) 

    'Link text file created by flow compressor using "TextFile_" and todays date 
    DoCmd.TransferText acLinkDelim, "TextFile20160126 Link Specification", _ 
"TextFile_" & FormDate, txtPath & "TextFile_" & FormDate & ".txt" 

    'Format recently linked FormatTextFlows into tables 
    Call FormatTextFlows 
    Me.Text41 = "Files Completed" 

End If 

下面是一個批處理文件,它調用:

U: 
cd "My Folder\test\FormatTextFlows\TempTextFlows" 

echo Date format = %date% 
echo dd = %date:~0,2% 
echo mm = %date:~3,2% 
echo yyyy = %date:~6,4% 
echo. 
echo Timestamp = %date:~6,4%-%date:~3,2%-%date:~0,2% 

copy *.USR "My Folder\test\FormatTextFlows\Data_TextFile\TextFile_%date:~6,4%%date:~3,2%%date:~0,2%.txt" 

預先感謝任何幫助任何人都可以提供。

+1

您不能在UNC路徑上使用CD命令;如果需要更改活動目錄,則可以將UNC路徑掛載到驅動器盤符並以此方式訪問它。如果您可以將基本UNC路徑放入一個變量,那麼您可以使用它來構建批處理文件中每個對象的完整路徑。 – CoveGeek

+0

Hi CoveGeek!這就解釋了爲什麼我不能調用它,只是不斷收到相同的錯誤信息。我實際上改變了'調用Shell(...'只是'Variable = Shell(...'而且似乎可行) – Wowdude

+0

當你通過shell命令傳遞一個帶有空格的路徑時,你需要確保在那裏使用chr(34)可以爲參數添加一個引號字符,以便命令行解釋器將它視爲單個參數。如果我有更多時間,我可以處理一個完整的例如對於vbs和cmd部分 – CoveGeek

回答

1

當你有空間,你需要報價:

U: 
cd "My Folder\test\FormatTextFlows\TempTextFlows" 

但是使用這個,我想這應該閱讀:

copy *.USR "U:\My Folder\test\FormatTextFlows\Data_TextFile\TextFile_%date:~6,4%%date:~3,2%%date:~0,2%.txt" 

您可能還需要在Shell命令報價:

Call Shell(Environ$("COMSPEC") & " /c " & Chr(34) & batpath & "myBat.bat" & Chr(34), vbNormalFocus) 

調試:

'Link text file created by flow compressor using "TextFile_" and todays date 
Debug.Print "TextFile20160126 Link Specification", _ 
"TextFile_" & FormDate, txtPath & "TextFile_" & FormDate & ".txt" 
DoCmd.TransferText acLinkDelim, "TextFile20160126 Link Specification", _ 
"TextFile_" & FormDate, txtPath & "TextFile_" & FormDate & ".txt" 

要檢查殼牌電話:

Debug.Print Environ$("COMSPEC") & " /c " & Chr(34) & batpath & "myBat.bat" & Chr(34) 
Call Shell(Environ$("COMSPEC") & " /c " & Chr(34) & batpath & "myBat.bat" & Chr(34), vbNormalFocus) 
+0

嗨古斯塔夫!感謝您的建議。我在文件路徑中添加了「」,並添加到複製目標的「U:\ ...」中, m仍然收到CMD中的相同消息,並且批處理文件不運行,我猜測它找不到.bat。 – Wowdude

+0

好吧,錯過了。請參閱使用Shell調用編輯 – Gustav

+0

再次Gustav!我把Chr(34)加入到你給我的地方,但是CMD仍然把路徑切割到路徑的根和第一個字:( – Wowdude

相關問題