Microsoft KB Archive/96591

= ACC: Determining How Many Instances of Application Are Active =

Article ID: 96591

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 Q96591



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:

167843 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 &quot;Introduction to Programming&quot; manual in Microsoft Access version 1.x, or the &quot;Building Applications&quot; manual, Chapter 3, &quot;Introducing Access Basic&quot; 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 &quot;Kernel&quot; (ByVal lpModuleName$) Declare Function GetModuleUsage% Lib &quot;Kernel&quot; (ByVal hModule%)

  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$ = &quot;&quot; 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 = &quot;AppToRun is presently active.&quot; End Function

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

  ?RunMyApp$(&quot;Clock.exe&quot;)

  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   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 &quot;Unable to start the Application&quot; End If  End Sub </ol>

Additional query words: multiple detect more than one

Keywords: kbhowto kbprogramming KB96591

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.