ACC: Determining How Many Instances of Application Are ActiveID: Q167843
|
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 do 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 ActiveThis 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.
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.
'------------------------------------------
' 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
' 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
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
For more information about the GetWindow, GetWindowText and IsWindowVisible API procedures, refer to the Win32 SDK.
Additional query words:
Keywords : kbinterop kbprg
Version : WINDOWS:7.0,97
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: August 5, 1999