Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

ACC2000: Determining How Many Instances of Application Are Active


View products that this article applies to.

This article was previously published under Q197593
Advanced: Requires expert coding, interoperability, and multiuser skills.

↑ Back to the top


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:
96591� 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:
167843� ACC: Determining How Many Instances of Application Are Active
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers 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 requirements.

↑ Back to the top


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:
    '------------------------------------------
    ' 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 2000 and open the TestAPI database. Note that you receive a message, and then the instance of Microsoft Access closes.

↑ Back to the top


References

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

↑ Back to the top


Keywords: KB197593, kbprogramming, kbinterop, kbhowto

↑ Back to the top

Article Info
Article ID : 197593
Revision : 5
Created on : 10/11/2006
Published on : 10/11/2006
Exists online : False
Views : 519