ACC: How to Wait for a Shelled Process to Finish

Last reviewed: June 8, 1997
Article ID: Q99940
The information in this article applies to:
  • Microsoft Access versions 1.0, 1.1, 2.0

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 "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 remain until you terminate them by typing "exit" (without the quotations marks) at the command line and pressing ENTER.

REFERENCES

For more information about using the Shell function in Microsoft Access 7.0 and 97, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q129796
   TITLE     : How a 32-Bit App Can Determine When a Shelled Process Ends

Microsoft Windows "Programmer's Reference, Volume 2: Functions," version 3.1, pages 403-404


Additional query words: terminate basic shell
Keywords : IntpShell kbinterop kbprg PgmApi PgmOthr
Version : 1.0 1.1 2.0
Platform : WINDOWS
Hardware : X86
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 8, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.