ACC2000: Determining How Many Instances of Application Are Active

ID: Q197593


The information in this article applies to:
  • Microsoft Access 2000

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


SUMMARY

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 on doing this in Microsoft Access 1.x and 2.0, please see the following article in the Microsoft Knowledge Base:

Q96591 ACC: Determining How Many Instances of Application Are Active
For information about doing this in Microsoft Access 7.0 and 97, please see the following article in the Microsoft Knowledge Base:
Q167843 ACC: Determining How Many Instances of Application Are Active
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp


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 2000 and create a new, blank database called TestAPI.mdb.


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


  3. 
    '------------------------------------------
    ' 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 
  4. Type the following procedures:


  5. 
    ' 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 
  6. Save the module as Module1 and close it.


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


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


  9. 
    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 
  10. Save the form as frmStartup and close it.


  11. On the Tools menu, click Startup.


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


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


  14. Start another instance of Microsoft Access 2000 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.

Additional query words:

Keywords : kbinterop kbprg kbdta AccCon KbVBA
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: July 15, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.