ACC: Determining How Many Instances of Application Are Active

Last reviewed: June 8, 1997
Article ID: Q96591
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.

You can use Windows API calls in Access Basic to determine how many instances of an application are running. You can use this to prevent re- entrance of an application that is already running, to determine how many instances of an application are active, or to start a loop in which you remain until the application quits.

For information about how to determine the number of active instances of Microsoft Access 7.0 or 97, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q167843
   TITLE     : ACC: Determining How Many Instances of Application Are
               Active

This article assumes that you are familiar with Access Basic and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information on Access Basic, please refer to the "Introduction to Programming" manual in Microsoft Access version 1.x, or the "Building Applications" manual, Chapter 3, "Introducing Access Basic" in version 2.0.

MORE INFORMATION

This article outlines three uses of two Windows APIs in Access Basic.

Open a new module or a previously created module and enter the following code:

NOTE: In the following sample code, an underscore (_) is used as a line- continuation character. Remove the underscore from the end of the line when re-creating this code in Access Basic.

   '------------------------------------------
   'Global Declarations Section Of The Module.
   '------------------------------------------
   Option Explicit

   Declare Function GetModuleHandle% Lib "Kernel" (ByVal lpModuleName$)
   Declare Function GetModuleUsage% Lib "Kernel" (ByVal hModule%)

   1. Prevent re-entrance:

   '------------------------------------------------------------------
   ' The following function will test for re-entrance and return
   ' True if there is already an instance of the application active,
   ' or it returns False. This function requires an argument that is
   ' the name of the application.
   '------------------------------------------------------------------
      Function TestForReentrance% (ApplicationName$)
         If ApplicationName$ = "" Then Exit Function
         If GetModuleUsage(GetModuleHandle(ApplicationName$)) Then _
                           TestForReentrance = True
      End Function

Example of Using this Code

You can use this function to keep from starting an application that is already running

   Function RunMyApp$ (szAppToRun$)
      Dim x%
      If Not TestForReentrance(szAppToRun) Then
         x = Shell(szAppToRun)
         Exit Function
      End If
      RunMyApp = "AppToRun is presently active."
   End Function

   and a call to this in the Immediate window such as:

   ?RunMyApp$("Clock.exe")

   2. Count the number of instances:

   '------------------------------------------------------------------
   ' The following function will return the number of instances of an
   ' application that are currently active.
   '------------------------------------------------------------------

   Function CountInstance% (szAppName$)
       CountInstance = GetModuleUsage(GetModuleHandle(szAppName))
   End Function

   3. Remain idle until an application quits:

   '------------------------------------------------------------------
   ' The following function will attempt to start an MS-DOS application
   ' and remain in a loop until that application quits.
   '------------------------------------------------------------------

   Sub RunDosAppUntil (szAppToRun$)
      Dim hMod%
      hMod% = Shell(szAppToRun, 1)
         If (hMod% > 32) Then
            While (GetModuleUsage(hMod%))
               DoEvents
            Wend
         Else
           MsgBox "Unable to start the Application"
        End If
   End Sub


Additional query words: multiple detect more than one
Keywords : kbprg PgmApi
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.