ACC: Determining How Many Instances of Application Are Active

Last reviewed: November 12, 1997
Article ID: Q167843
The information in this article applies to:
  • Microsoft Access versions 7.0, 97

SUMMARY

Advanced: Requires expert coding, interoperability, and multiuser skills.

You can use Windows API calls in Visual Basic for Applications code to determine how many instances of an application are running. Then you can use the information to prevent re-entrance of an application that is already running.

For information about how to accomplish this in Microsoft Access 1.x and 2.0, please see the following article in the Microsoft Knowledge Base:

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

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.

MORE INFORMATION

The following example uses a procedure in the Open event of a startup form to determine if Microsoft Access is already running. If it is running, a message advises the user, and then the second instance of the program closes.

  1. Start Microsoft Access and create a new, blank database called
     TestAPI.mdb.

  2. Create a module and type the following lines in the Declarations
     section:

      '------------------------------------------
      ' Global Declarations Section Of The Module
      '------------------------------------------

      Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, _
      ByVal wCmd As Long) As Long

      Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
      (ByVal hwnd As Long, ByVal lpString As String, ByVal CCh As Long) _
      As Long

      Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) _
      As Long

      Public Const GW_HWNDFIRST = 0
      Public Const GW_HWNDLAST  = 1
      Public Const GW_HWNDNEXT  = 2
      Public Const GW_HWNDPREV  = 3

  3. Type the following procedures:

      ' This function returns the Caption Text of each window passed to
      ' it. If a window does not have a Caption bar, then this function
      ' returns a zero-length string ("")
      Function GetAppName(Lnghwnd as long)
         Dim LngResult As Long
         Dim StrWinText As String * 255
         Dim LngCCh As Long
         LngResult = GetWindowText(Lnghwnd, StrWinText, 255)
         GetAppName = Left(StrWinText, LngResult)
      End Function

      ' This function counts all instances of an application that are open,
      ' including any windows that are not visible.
      ' Arguments: LngHwnd        = Any valid window handle.
      '            StrAppCaption  = The window caption to search for.
      ' Example:   GetCountOfWindows(hWndAccessApp,"Microsoft Access")
      Function GetCountOfWindows(Lnghwnd, StrAppCaption)
         Dim LngResult As Long
         Dim LngICount As Long
         Dim StrAppName As String

         LngResult = GetWindow(Lnghwnd, GW_HWNDFIRST)
         Do Until LngResult = 0
            If IsWindowVisible(LngResult) Then
               StrAppName = GetAppName(LngResult)
               If InStr(1, StrAppName, StrAppCaption) Then
                  LngICount = LngICount + 1
               End If
            End If
            LngResult = GetWindow(LngResult, GW_HWNDNEXT)
         Loop
         GetCountOfWindows = LngICount
         End Function

  4. Save the module as Module1 and close it.

  5. Create a new form not based on any table or query in Design view.

  6. Set the OnOpen property of the form to the following event procedure:

      Private Sub Form_Open(Cancel As Integer)
      If GetCountOfWindows(hWndAccessApp, "Microsoft Access") > 1 Then
         Cancel = True
         MsgBox "Please use the instance of Microsoft Access that is " _
                & "already open."
         DoCmd.Quit acQuitSaveNone
      End If
      End Sub

  7. Save the form as frmStartup and close it.

  8. On the Tools menu, click Startup.

  9. In the Startup dialog box, select frmStartup in the Display Form box,
     and then click OK.

 10. Close and then reopen the TestAPI database non-exclusively. Note that
     the frmStartup form appears.

 11. Start another instance of Microsoft Access and open the TestAPI
     database. Note that you receive a message, and then the instance of
     Microsoft Access closes.

REFERENCES

For more information about the GetWindow, GetWindowText and IsWindowVisible API procedures, refer to the Win32 SDK.

Keywords          : kbinterop kbprg
Version           : 7.0 97
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: November 12, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.