Microsoft KB Archive/99940

= ACC: How to Wait for a Shelled Process to Finish =

Article ID: 99940

Article Last Modified on 5/9/2003

-

APPLIES TO


 * Microsoft Access 1.0 Standard Edition
 * Microsoft Access 1.1 Standard Edition
 * Microsoft Access 2.0 Standard Edition

-



This article was previously published under Q99940



SUMMARY
Advanced: Requires expert coding, interoperability, and multiuser skills.

When you are using the Shell function or the RunApp macro action to run another program or process, Microsoft Access does not wait for the shelled process to finish before processing the next line in the macro or function. This causes problems for macros and functions whose subsequent actions depend on the results of the shelled process.

This article demonstrates a Microsoft Access function called WaitShell that, given an application name or process to run, starts the process and waits for it to terminate.



MORE INFORMATION
The following Microsoft Access function, WaitShell, uses GetModuleUsage, a Microsoft Windows API function, to determine if the shelled process has terminated. The Shell function, if it successfully starts the process, returns a handle to the process module. If the process is no longer running, the handle is invalid and the GetModuleUsage function returns a value of 0. The WaitShell function simply loops until the module is no longer valid.

NOTE: You may have the following GetModuleUsage Windows API function defined in an existing Microsoft Access library. If you receive a duplicate procedure name error message, delete the Declare statement from your code.

  Option Explicit Declare Function GetModuleUsage% Lib &quot;Kernel&quot; (ByVal hModule%)

Function WaitShell( AppName as String) Dim hMod as Integer hMod = Shell(AppName, 1) If (Abs(hMod) > 32) then While (GetModuleUsage(hMod)) DoEvents Wend Else MsgBox &quot;Unable to start &quot; & AppName End If  End Function

To test the function, create and run the following Microsoft Access macro:

Action --     RunCode MsgBox

Macro Actions ---     RunCode Function Name: =WaitShell(&quot;Command.com&quot;) MsgBox Message: Done!

-or-

run the following Microsoft Access function:

Function TestWaitShell x=WaitShell(&quot;Command.com&quot;) MsgBox &quot;Done!&quot; End Function

Both examples above start instances of the MS-DOS prompt, which remain until you terminate them by typing &quot;exit&quot; (without the quotations marks) at the command line and pressing ENTER.

