Willard said:
I have a frontend/backend system at a non-profit. Users entering
intake interview info are not always computer savvy. I frequently
find that they have opened the frontend more than once....even 4
times once. I'd like to be able to prevent this. I've found some
logic that will prevent the opening of any other Access db, but some
users use more than one Access system and I do not want them to have
to close their other systems when they open mine. Is there a way
vbwise to see if a particular .mdb is already open?
Best practice for this is to create a mutex using the according
Windows APIs CreateMutex, OpenMutex and ReleaseMutex.
In a standard module:
-----------------------------------------
Option Explicit
'Put your own unique application-name here
Public Const APPLICATION_NAME As String = "TestAppMutexTest"
Private Const READ_CONTROL = &H20000
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Private Declare Function CreateMutex Lib "kernel32" Alias "CreateMutexA" ( _
lpMutexAttributes As SECURITY_ATTRIBUTES, _
ByVal bInitialOwner As Long, _
ByVal lpName As String) As Long
Private Declare Function OpenMutex Lib "kernel32" Alias "OpenMutexA" ( _
ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal lpName As String) As Long
Private Declare Function ReleaseMutex Lib "kernel32" ( _
ByVal hMutex As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" ( _
ByVal hHandle As Long) As Long
Public ApplicationMutex As Long
Public Function MutexCreate() As Boolean
Dim lhandle As Long
Dim SecAttrib As SECURITY_ATTRIBUTES
'-------------------------
With SecAttrib
.nLength = Len(SecAttrib)
.lpSecurityDescriptor = 0&
.bInheritHandle = 0&
End With
lhandle = OpenMutex(READ_CONTROL, 0&, APPLICATION_NAME)
If lhandle > 0 Then
CloseHandle lhandle
MutexCreate = False
Else
ApplicationMutex = CreateMutex(SecAttrib, 1&, APPLICATION_NAME)
MutexCreate = (ApplicationMutex > 0)
End If
End Function
Public Sub MutexRelease()
If ApplicationMutex > 0 Then
ReleaseMutex ApplicationMutex
CloseHandle ApplicationMutex
ApplicationMutex = 0
End If
End Sub
-----------------------------------------
in a start form:
-----------------------------------------
Option Explicit
Private Sub Form_Open(Cancel As Integer)
If MutexCreate() = False Then
MsgBox "Sorry, the Applikation '" & APPLICATION_NAME & "' is already
running.", _
vbExclamation, APPLICATION_NAME
DoCmd.Quit acQuitSaveAll
End If
End Sub
Private Sub Form_Close()
MutexRelease
End Sub