View Single Post
  #4 (permalink)  
Old 12-06-03, 21:30
CyberLynx CyberLynx is offline
Stuck on my opinions...
 
Join Date: Nov 2003
Posts: 1,487
This is what you want......
Here, we show you how to shell to another program from Access, stop your code while the shelled process operates and then resume your code once the process is finished. To do this you need to use the API functions:

'WaitforSingleObject' to make the wait state
'OpenProcess' to launch a shelled process and wait for it to complete.
'CloseHandle' to close the process.

Listed below is the code to use:

1. On the declarations page of your module, add the following functions:
Code:
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal _
     dwAccess As Long, ByVal fInherit As Integer, ByVal hObject _
     As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
      hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal _
      hObject As Long) As Long
2. Try out this test function, which launches any app you want to and waits until it is finished to display a message box (Note an " _ " underscore means line continuation):

Code:
Function LaunchApp32 (MYAppname As String) As Integer
 On Error Resume Next
 Const SYNCHRONIZE = 1048576
 Const INFINITE = -1&
 Dim ProcessID&
 Dim ProcessHandle&
 Dim Ret&

 LaunchApp32=-1
 ProcessID = Shell(MyAppName, vbNormalFocus)
   If ProcessID<>0 then
       ProcessHandle = OpenProcess(SYNCHRONIZE, True, ProcessID&)
       Ret = WaitForSingleObject(ProcessHandle, INFINITE)
       Ret = CloseHandle(ProcessHandle)
  
       MsgBox "This code waited to execute until " _ 
          & MyAppName & " Finished",64
   Else
        MsgBox "ERROR : Unable to start " & MyAppname
        LaunchApp32=0
   End If
End Function
3. It's very important to note that your function must include the code to close the process handle after the shelled application is complete, otherwise you will have a memory leak until you shut down Windows.

Enjoy
Reply With Quote