Prevent Multiple Instances

Rob.Mills

Registered User.
Local time
Today, 15:06
Joined
Aug 29, 2002
Messages
871
Is there a way to prevent a user from opening multiple instances of a database?
 
If it is only a single user who needs to use the db then they could open it exclusively and that will prevent anybody else from opening the db. You would need to use a custom shortcut to do that. Here is a sample Target: field...

"C:\Program Files\Microsoft Office\Office\msaccess.exe" "X:\Testing\YourDB.mdb" /excl

HTH
 
Not the case. Multiple users hooked up at the same time. What I'm finding is they're opening multiples so that they can have different forms open at the same time and it's causing a lot of headaches.
 
The db looks good but I can not get it to work. It compiles without errors. I am using Access 97 and the main part of the CheckRestarting() is not firing. I added message boxes and nothing is tripping the IF...
Code:
    If lngWndApp <> 0 Then
        apiSetActiveWindow lngWndApp
[COLOR=blue]        MsgBox "apiSetActiveWindow lngWndApp"[/COLOR]
        If apiIsIconic(lngWndApp) Then
            apiShowWindowAsync lngWndApp, SW_RESTORE
[COLOR=blue]            MsgBox "apiShowWindowAsync [/COLOR]lngWndApp, SW_RESTORE"
        Else
            apiShowWindowAsync lngWndApp, SW_SHOW
[COLOR=blue]            MsgBox "apiShowWindowAsync [/COLOR]lngWndApp, SW_SHOW"
        End If
        Application.Quit
[COLOR=blue]        MsgBox "Application.Quit"[/COLOR]
    End If
Any suggestions?
 
I’ll have to have a closer look at this because it was written a few years ago.

I’m currently using A2K under Win2000 and the AutoExec Macro is not firing on a second start. However I still don’t get a second copy opening, it simply maximizes the copy on the task bar.

Regards,
Chris.
 
While Chris troubleshoots his upload, I'll offer a couple of alternative plans of attack to consider.

Compel the users to log on with a username, validating the name against a backend table you create. If their username is not in the table, add it and let them into the app. When they quit the app, delete their record from the table. Or, if you don't want users to bother logging in to your Access DB, you can use the user's network login name, assuming they need that to get on their workstation and, too, assuming each of them has a unique username; keep in mind that this will wed your project to the whims of your network admins. (Or, given your needs, you may want to use the computername in place of the username.)

Or, maybe easiest of all, on start up of your app, check for the existence of simple text file on the local hard disk. If the file isn't present, create it (you can use the MS scripting object to do this) and then open the app fully. If it is present, stop the user from entering. When the app is closed, be sure to delete the file. If your users are in the habit of shutting down their computers in the middle of your app, you will probably have to build a secondary utility that allows them (or a net admin) to "clear" this file from their disk...

Regards,
Tim
 
Last edited:
Thanks Tim because under my current circumstances I cannot get a duplicate copy of any database to open which means, at the moment, none of that code would be required.

What I do know is I was having the same problem that Rob.Mills is currently having. Two of my customers were in the habit of minimising the application, perhaps to look at their e-mails, and when finished would double click the application start shortcut they had on the desktop. They got duplicate copies which caused them some grief.

As an aside…
On one occasion I drove 1400km because one of my customers was using the period on the numeric keypad as the date separator. (I couldn’t pick that one over the phone. :o ) While I was there I noticed how they were using the FE, or should I say multiple copies of the FE, which doesn’t come under the heading of best practice. I was there 5 weeks modifying the system free of charge, but at $200 a night free accommodation at their resort on Magnetic Island, didn’t think charging them was appropriate. The modifications were subsequently sold to the other user on the drive back home, with a week’s free accommodation at their resort of course. Ah, the good old days. :cool: :D

Returning to subject…
Both users are running mde FE/BE in the same directory, and no multi users. Both are running A97, Office97 and Win98SE. I could duplicate the problem on my machine, which was at the time, A97, Office97 and Win2K and that is why the code is there.

Under A2K, Office2K and Win2K I cannot get a database to start multiple copies, all it does is to maximize the db that is already open. I guess, since no new copy is opened, the AutoExec Macro does not fire, but then again…it does not need to fire.

If the code fixes Rob.Mills needs then so be it, otherwise he may need something else as you have described.

At the moment I’m stumped.

Regards,
Chris.
 
Last edited:
Chris,

I know Magnetic Island is an attractive place but next time charge the client a daily fee as well as reimbursement for petrol -- that's a long haul. Also, like you, I can't open two instances of Access that run the same Access 2k file on the same PC either (W2K). I can, however, open two identical files, same name, one a copy of the other, in two separate instances of Access if those two files are located in separate folders.

Another thing -- Rob -- you may want to ultimately give the users what they want, updating your DB so they can do whatever it is they do in one instance rather than two. Assuming, in this instance, if you like them.

Regards,
Tim
 
Tim got there before me. If your users need multiple forms open, then let them have it!

If they don't need it, it's a training issue.
 
This was the first time I got to look at the posts since my last reply. Bad weather where I'm at. Thanks for all the replies. I'm going to try some out right now.

Originally I set up the db so they could only open one form at a time and so I understood why they opened multiple copies. But then I changed it so they could open all the forms they wanted. I don't think they're opening multiples on purpose. I think some of them are just losing track where the icon is in the taskbar and opening another cause they think they closed it.
 
I did this a while back:

Code:
'Checks to see if there is another instance of OSR+ open
'If Return value is false then there is an instance already open
'Written by Chris O'Donoghue 22/11/00

Dim blnSwitch As Boolean
Dim strDBCaption As String
Dim lnghWndApp As Long
Dim lnghWndDB As Long

Const cstrProc = "OSR Instance Checker"

    On Error GoTo Err_OSR_Instance_Check
    
    'Gets the names of the file that are Access Instances
    strDBCaption = winGetTitle(winGetHWndDB())
    lnghWndApp = apiGetWindow(apiGetDesktopWindow(), GW_CHILD)
    
    'Checks to see if any of the other instances have the same name
    'as the current one
    Do Until lnghWndApp = 0
        If lnghWndApp <> Application.hWndAccessApp Then
            lnghWndDB = winGetHWndDB(lnghWndApp)
            If lnghWndDB <> 0 Then
                If strDBCaption = winGetTitle(lnghWndDB) Then Exit Do 'More than one instance with same name
            End If
        End If
        lnghWndApp = apiGetWindow(lnghWndApp, GW_HWNDNEXT)
    Loop
    
    'Determines if they are the same
    If lnghWndApp = 0 Then
        OSR_Instance_Check = True 'Current Instance is the only instance
        GoTo Exit_OSR_Instance_Check
    End If
    
    If blnMsg Then
        MsgBox strDBCaption & " is already open@" _
        & "You cannot open a second instance of this database@" _
        & " OSR+ will now quit", vbOKOnly + vbExclamation, "Multiple Instance Error"
    End If
    
    apiSetActiveWindow lnghWndApp 'Makes sure that the original instance of OSR+ is the active window
    
    If apiIsIconic(lnghWndApp) Then
        apiShowWindowAsync lnghWndApp, SW_RESTORE 'If it's minimized, restore it
    Else
        apiShowWindowAsync lnghWndApp, SW_SHOW 'If it's not minimized, show it
    End If


You'll also need the api's (available from www.mvps.org/access)
 
I just tested the code at the below link and it works with Access 97. It all fits into one module with no external calls or references.

API: Preventing multiple instances of a database

I just added this command to an AutoExec macro:
Action: RunCode
Function Name: =winCheckMultipleInstances(False)

And you can briefly see the second instance of the db open and then close just as quickly.
 

Users who are viewing this thread

Back
Top Bottom