ACC: Determining How Many Instances of Application Are Active

ID: Q96591


The information in this article applies to:


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:

Q167843 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 


  2. 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") 

  3. 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 


  4. 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 
Version           : 1.0 1.1 2.0
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: March 19, 1999