Multiple Instance Handling

Posted on 2004-07-31
Last Modified: 2006-11-17
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
Question by:miqrogroove
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
LVL 32

Expert Comment

ID: 11684715
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.
LVL 32

Expert Comment

ID: 11684761
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.
LVL 11

Author Comment

ID: 11685201
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.  : /
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

LVL 32

Expert Comment

ID: 11685391
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.
LVL 11

Author Comment

ID: 11685411
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 : )
LVL 11

Author Comment

ID: 11685656
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?
LVL 18

Expert Comment

ID: 11688759
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
LVL 11

Author Comment

ID: 11992926
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
LVL 11

Author Comment

ID: 11992958
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.
LVL 32

Accepted Solution

jadedata earned 500 total points
ID: 11996720
  you are too kind...
LVL 11

Author Comment

ID: 12107393
That is correct, bravo!  : )

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

697 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question