Multiple Instance Handling

I am developing an application with multi-user functionality in mind.  It currently has the mde/mdb split feature, and I am working on adding some data integrity routines that use formal database transactions and pessimistic locking to minimize confusion if two users try to edit the same information.

Despite the lengths to which I have gone for user-friendliness, I think it is still undesireable to have two instances of the application running on the same taskbar.  It is confusing to the user, even though it may be technically safe.

I am not completely satisfied with any solution I have found on so far.  This is probably among the best:

I agree with the possibility of checking the taskbar for a certain application title, but I feel this is a bit sloppy.  This solution requires that most of my Startup routines be complete (including the part that sets the title bar) before performing the test and ultimately closing the unwanted new instance.  Yuck.  I would be worried about two instances starting simultaneously and destroying each other.

Since I have a split database, it occurs to me that I should simply require the mde application to be opened in exclusive mode.  Problem solved, right?  Well, it's not quite that simple.  In theory, the application should be structured to allow multiple connections to the mdb file and only one at a time to the local mde file.  From what I have read, it is only possible to do this by checking for the presence of an ldb file in the mde's folder.  Is there any better way to force the database application to be opened exclusively?  What about application lockups?  Would orphaned ldb files be a regular headache?

Then there's the issue of Fast User Switching, RDP, etc.  (Yes, if your head starts to spin at this point it may be normal.)  Did you figure out the problem?  Let's say for the sake of simplicity that each user of the application has the mde file stored in their home folder so that each user may open the application exclusively on the same computer.  Problem solved?  Perhaps.  I have not yet tested this configuration on Windows XP.

Again, my primary concern is that any one user might have unknowingly started a second copy of the application and become confused by locking errors while editing records or by unexpected results while switching between tasks.

I would like to have some discussion on this topic rather than have a single best solution.  I will split out the points for anyone adding substance to the topic.

-- Miqro
LVL 11
Who is Participating?
jadedataConnect With a Mentor MS Access Systems CreatorCommented:
  you are too kind...
jadedataMS Access Systems CreatorCommented:
at this link is a bit of code that will prevent a user on a single machine from opening more than 1 instance of an Access project.
This code is in ALL of my current production applications.
You can include this in the main form opener to test for an existing occurance of "itself" and shut down if found.  

ps:  I don't consider running two occurances of the SAME File.mdb SAFE.  This is mostly due to the types of features that I program into my interfaces that require user settings to control the application session.
jadedataMS Access Systems CreatorCommented:
well api0041 was already covered by Lena on the last question.  so nevermind...

Your assumptions on how connections are made to databases are only partially correct.  LDB files are for control of sharing data and object handles in any database built with MSAccess including your mdw files.  Any file touched by a user opens an LDB.

The bottom line is that each user should be opening THEIR OWN copy of the MDB/MDE (application front end) to prevent object use collisions.  No two users should be opening the same front end from the same location simultaneously. (IMHO)  Likewise the same user should not be launching multiple copies of the same file.  In launching different files like different versions, the api0041 would prevent the start of the second one based on the Project Name presuming that that does not change across versions.

Using the api0041 package in your application will prevent users from launching the application more than once based on its' PROJECT NAME not the name found on the taskbar which can vary as you navigate thru the application.
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

miqrogrooveAuthor Commented:
jadedata, databases opened exclusively in Access XP do not have ldb files.  I tried it myself.  The question is, can the database be forced into exclusive mode without using the easily-bypassed shortcut system, or the clumsy File Open dialog?  I find it frustrating that the Recently Used File List always opens the database in shared mode.  : /
jadedataMS Access Systems CreatorCommented:
opening in exclusive mode only applies to the database you select, not to database that are referenced or attached to.  

Opening the front end in exclusive mode shouldn't have any impact on anything if you are not having more than one person open a copy of the frontend.  The existance of an LDB is a moot point.

"The question is, can the database be forced into exclusive mode without using the easily-bypassed shortcut system, or the clumsy File Open dialog?"
No.  MS Access does not provide a means of doing this that way.

"I find it frustrating that the Recently Used File List always opens the database in shared mode.  : /"

MSAccess is not a database server application.  It is a database container.  Security was an after thought.  The features you are looking for have always been in MSAccess's bigger brother packages that are REAL database applications.  Access is database for the masses and therefore has the limitations that it does.
miqrogrooveAuthor Commented:
The LDB file is not moot because a user who has properly opened their front-end application in exclusive mode cannot open a second instance.  That is the whole point of this thread : )
miqrogrooveAuthor Commented:
I also want to bring up the OpenSchema method of the CurrentProject.Connection property.  If not using the Open Exclusive idea I proposed earlier, then wouldn't it be a lot simpler to check for multiple connections to the mde file instead of multiple taskbar instances of the application?
I saw some code in Access/VB Advisor a while back and adapted it to fit my works great.

If you user attempts to open another instance of the database, it closes the database they tried to open and sets the focus to the instance already running.  Put all the code below in a standard module and then call the function at startup.  You will need to change the name of the project to yours....Mike

'Used to set the focus to the current instance of the application
Public Declare Function apiShowWindow Lib "user32" Alias "ShowWindow" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Public Const SW_MAXIMIZE = 3

'Used to check to see if the application is currently running
Private Declare Function apiFindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

'Used to change the caption of the current app to something else while we perform the check
Private Declare Function apiSetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long

Public Function MTSBSITestInstance()
    Dim lngRunning As Long
    Dim lngCurrentHWnd As Long
    Dim lngTest As Long

    'Check to see if the application is running
    lngRunning = apiFindWindow(vbNullString, "Banquet Balance")
    If lngRunning = 0 Then
        'Not currently running
        'Get the current handle of this window
        lngCurrentHWnd = apiFindWindow(vbNullString, "Balance")
        lngTest = apiSetWindowText(lngCurrentHWnd, "Banquet Balance")
        'Set the focus to the handle of the already running application
        lngTest = apiShowWindow(lngRunning, SW_MAXIMIZE)
        'Quit this application
    End If
End Function
miqrogrooveAuthor Commented:
I have been using the following code with great success so far.  I still think it could be improved, but it does achieve the end result with a minimum of fuss (I completely hate the idea of checking window names and using API calls for such a simple task)

Option Explicit

(This is my Startup Form's Load Event Sub)

    Dim UserList As ADODB.Recordset
    Dim UserCount As Long
    UserCount = 0
    Set UserList = CurrentProject.Connection.OpenSchema(adSchemaProviderSpecific, , _
    Do While Not UserList.EOF
        If UserList(2).Value Then UserCount = UserCount + 1
    Set UserList = Nothing
    If UserCount > 1 Then
        DoCmd.Close acForm, Me.Name, acSaveNo
        Application.SysCmd acSysCmdRemoveMeter
        DoCmd.Hourglass False
        MsgBox "You attempted to open multiple instances of this application file." & vbCrLf & vbCrLf & _
               "To prevent technical difficulties, this window will now be closed.", vbInformation + vbOKOnly, _
               "Multiple Instances Are Not Allowed"
        Application.Quit acQuitPrompt
        Exit Sub  'Code will not be executed beyond this point.
    End If

If no other comments are added to this thread, then I believe this routine I wrote should be the correct answer, for lack of a better one.

-- Miqro
miqrogrooveAuthor Commented:
There will also be some points for jadedata..  Though I realize Access has its limitations, I am developing an Access application atm.  In the future, I will probably dive into a real client/server solution that will give me full control over these little issues.
miqrogrooveAuthor Commented:
That is correct, bravo!  : )
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.