Solved

Multiple Instance Handling

Posted on 2004-07-31
11
782 Views
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 experts-exchange.com so far.  This is probably among the best:

http://www.experts-exchange.com/Databases/MS_Access/Q_21009739.html

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.

Thanks,
-- Miqro
0
Comment
Question by:miqrogroove
  • 6
  • 4
11 Comments
 
LVL 32

Expert Comment

by:jadedata
ID: 11684715
http://www.mvps.org/access/api/api0041.htm
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.
0
 
LVL 32

Expert Comment

by:jadedata
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 MSPS.org 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.
0
 
LVL 11

Author Comment

by:miqrogroove
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.  : /
0
 
LVL 32

Expert Comment

by:jadedata
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.  : /"
Sorry.

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.
0
 
LVL 11

Author Comment

by:miqrogroove
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 : )
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 11

Author Comment

by:miqrogroove
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?
0
 
LVL 18

Expert Comment

by:Data-Man
ID: 11688759
I saw some code in Access/VB Advisor a while back and adapted it to fit my needs....it 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")
    Else
   
        'Set the focus to the handle of the already running application
        lngTest = apiShowWindow(lngRunning, SW_MAXIMIZE)
       
        'Quit this application
        Application.Quit
       
    End If
End Function
0
 
LVL 11

Author Comment

by:miqrogroove
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, , _
                   "{947bb102-5d43-11d1-bdbf-00c04fb92675}")
    Do While Not UserList.EOF
        If UserList(2).Value Then UserCount = UserCount + 1
        UserList.MoveNext
    Loop
    UserList.Close
    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
0
 
LVL 11

Author Comment

by:miqrogroove
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.
0
 
LVL 32

Accepted Solution

by:
jadedata earned 500 total points
ID: 11996720
miqrogroove:
  you are too kind...
-j-
0
 
LVL 11

Author Comment

by:miqrogroove
ID: 12107393
That is correct, bravo!  : )
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now