[AccessD] (A97) Prevent multiple instances of Access

Bob Gajewski bob at renaissancesiding.com
Thu Mar 13 07:59:00 CST 2003


Dear Fellow Listers:

I am in over my head (again)! I am trying to prevent users from opening a 
second instance of Microsoft Access (A97). The code below was taken 
directly from Microsoft's Knowledge Base (A97=167843, A2K=197593). It works 
*perfectly* - if the other instance is windowed; it doesn't work at all if 
the other instance is either minimized or maximized. I need it to work no 
matter what the state of the currently running instance.

I placed the CBF in the Switchboard, as my AutoExec macro loads the 
switchboard when the database is opened.

Any suggestions, of course, are greatly appreciated!

TIA,

Bob Gajewski


*** SWITCHBOARD CBF ***
===============================================
Private Sub Form_Open(Cancel As Integer)

 ' Check for instance of Access already running.

    If GetCountOfWindows(hWndAccessApp, "Microsoft Access") > 2 Then
    ' I changed the original value of 1 to 2, as the error condition 
occurred _
	on every initial open when the value was 1.
       Cancel = True
       MsgBox "Please use the instance of Microsoft Access that is " _
              & "already open."
       DoCmd.Quit acQuitSaveNone
    End If

End Sub
===============================================


*** MODULE "modStartup" ***
===============================================
Option Compare Database
Option Explicit

'------------------------------------------
' 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
===============================================







More information about the AccessD mailing list