This article was previously published under Q99940
This article has been archived. It is offered "as is" and will no longer be updated.
Advanced: Requires expert coding, interoperability, and multiuser skills.
When you are using the Shell() function or the RunApp macro action torun another program or process, Microsoft Access does not wait for theshelled process to finish before processing the next line in the macroor function. This causes problems for macros and functions whosesubsequent 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 andwaits for it to terminate.
The following Microsoft Access function, WaitShell(), usesGetModuleUsage(), a Microsoft Windows API function, to determine if theshelled process has terminated. The Shell() function, if it successfullystarts the process, returns a handle to the process module. If the processis no longer running, the handle is invalid and the GetModuleUsage()function returns a value of 0. The WaitShell() function simply loops untilthe module is no longer valid.
NOTE: You may have the following GetModuleUsage() Windows API functiondefined in an existing Microsoft Access library. If you receive aduplicate procedure name error message, delete the Declare statementfrom your code.
Option Explicit Declare Function GetModuleUsage% Lib "Kernel" (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 "Unable to start " & 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("Command.com") MsgBox Message: Done! -or- run the following Microsoft Access function: Function TestWaitShell() x=WaitShell("Command.com") MsgBox "Done!" End Function
Both examples above start instances of the MS-DOS prompt, which remainuntil you terminate them by typing "exit" (without the quotations marks)at the command line and pressing ENTER.
For more information about using the Shell function in Microsoft Access 7.0and 97, please see the following article in the Microsoft Knowledge Base: 178116 How to Determine When a Shelled Process Ends
Microsoft Windows "Programmer's Reference, Volume 2: Functions,"version 3.1, pages 403-404