What code can I insert in a module running several processes, that will allow a process to finish B4 moving to next process. Right now I am not getting desired results because 1 process is stepping on the other.
' this code was lifted from the internet.
' purpose: to shell out a command and wait until
' the command is done.
Option Compare Database
Option Explicit
Private Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type
Private Declare Function WaitForSingleObject Lib "kernel32" ( _
ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CreateProcessA Lib "kernel32" ( _
ByVal lpApplicationName As String, ByVal LpCommandLine As String, _
ByVal lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As String, _
lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, lpExitCode As Long) As Long
Public Function ExecCmd(CmdLine As String)
DoCmd.Hourglass True
Dim Proc As PROCESS_INFORMATION
Dim Start As STARTUPINFO
Start.cb = Len(Start)
Dim ret As Long
ret = CreateProcessA(vbNullString, CmdLine, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, vbNullString, Start, Proc)
ret = WaitForSingleObject(Proc.hProcess, INFINITE)
Call GetExitCodeProcess(Proc.hProcess, ret)
Call CloseHandle(Proc.hThread)
Call CloseHandle(Proc.hProcess)
ExecCmd = ret
DoCmd.Hourglass False
End Function
I wouldn't even know where to begin with this code. If this is the way to do this, can someone give me an indication where to place this code in the module.
1) Create a module
2) Paste the supplied code into the module
3) compile/save the module
4) use ExecCmd(<filename>) to run a program, e.g.
ExecCmd("c:\winnt\system32\notepad.exe"
ExecCmd will wait until the process is complete before returning control to your visual basic code.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.