批处理未完全从 Access 执行

问题描述 投票:0回答:1

我正在 VBA 中从 MSAccess 运行批处理文件。它没有完全执行。如果我在访问之外运行它,它就可以正常工作。我正在使用 VBA 创建 bat 文件,然后运行它并退出访问。然后bat文件会删除access文件,然后下载一个新文件并打开新文件。就像我说的,当我在访问之外运行 bat 文件时,它工作得很好。但从访问中它会删除当前文件,然后不再继续执行脚本的其余部分。欢迎任何指导。

访问代码:

Private Sub UpdateFrontEnd(ByVal LocalFilePath As String)
 
Dim BatchFile As String
Dim Restart As String
Dim ShellCommand As String

    BatchFile = CurrentProject.Path & "\updater.bat"
    
    Restart = """" & LocalFilePath & """"
    
    ShellCommand = "Invoke-WebRequest http://downloadipaddress/filename.accdb -OutFile filename.accdb"
 
    Open BatchFile For Output As #1
    Print #1, "@Echo Off"
    Print #1, "ECHO Deleting old file..."
    Print #1, ""
    Print #1, "ping 127.0.0.1 -n 5 -w 1000 > nul"
    Print #1, ""
    Print #1, "Del """ & LocalFilePath & """"
    Print #1, ""
    Print #1, "ECHO Downloading new file..."
    Print #1, "powershell -Command " & ShellCommand; ""
    Print #1, ""
    Print #1, "ECHO Starting Microsoft Access..."
    Print #1, "START /I " & """MSAccess.exe"" " & Restart
    Close #1
 
    Shell BatchFile
 
    DoCmd.Quit
End Sub

我厌倦了将它分成两个不同的bat文件并从第一个文件调用第二个文件,但结果仍然相同。

我也尝试过只运行bat而不是在VBA代码中创建它,但结果仍然相同。

vba ms-access batch-file
1个回答
0
投票

考虑在 Shell 命令中调用

cmd.exe
,模拟在 Access 外部双击批处理文件。另外,在删除和 PowerShell 命令之后运行 wait for 命令
ping
以允许这些步骤完成。最后考虑
Application.Quit
与遗留
DoCmd.Quit
的同义形式,这是每个 MS 文档的推荐版本

Private Sub UpdateFrontEnd(ByVal LocalFilePath As String)
 
    Dim BatchFile As String, Restart As String, ShellCommand As String

    BatchFile = CurrentProject.Path & "\updater.bat"
    
    Restart = """" & LocalFilePath & """"
    
    ShellCommand = "Invoke-WebRequest http://downloadipaddress/filename.accdb -OutFile filename.accdb"
 
    Open BatchFile For Output As #1
    Print #1, "@Echo Off"
    Print #1, ""
    Print #1, "ECHO Deleting old file..."
    Print #1, "Del """ & LocalFilePath & """"
    Print #1, ""
    Print #1, "ping 127.0.0.1 -n 5 -w 1000 > nul"
    Print #1, ""
    Print #1, "ECHO Downloading new file..."
    Print #1, "powershell -Command " & ShellCommand; ""
    Print #1, ""
    Print #1, "ping 127.0.0.1 -n 5 -w 1000 > nul"
    Print #1, ""
    Print #1, "ECHO Starting Microsoft Access..."
    Print #1, "START /I " & """MSAccess.exe"" " & Restart
    Close #1
 
    Shell "cmd.exe /c " & BatchFile, vbNormalFocus
    Application.Quit

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.