I have found that users sometimes minimize my application, and then later, start it "again" thus starting a new instance of the program.
I want to prevent this from happening.
Current setup:
Frontend is a MDE file.
Backend is mdb file on shared network drive.
Acccess Version: 2010
Windows 7
I found this code at http://access.mvps.org/access/api/api0041.htm.
The problem is I can't get it to work, the sMyCaption variable is set to "" thus it doesn't really matter what happens in the rest of the code. I have tried and tried to understand the API calls, but I am getting nowhere and my head starts to hurt! I would appreciate it if anyone with more API skills then me, can spot what might go wrong. - '******************** Code Start ********************
-
' Module mdlCheckMultipleInstances
-
' © Graham Mandeno, Alpha Solutions, Auckland, NZ
-
' graham@alpha.co.nz
-
' This code may be used and distributed freely on the condition
-
' that the above credit is included unchanged.
-
-
Private Const cMaxBuffer = 255
-
-
Private Declare Function apiGetClassName Lib "user32" _
-
Alias "GetClassNameA" _
-
(ByVal hWnd As Long, _
-
ByVal lpClassName As String, _
-
ByVal nMaxCount As Long) _
-
As Long
-
-
Private Declare Function apiGetDesktopWindow Lib "user32" _
-
Alias "GetDesktopWindow" _
-
() As Long
-
-
Private Declare Function apiGetWindow Lib "user32" _
-
Alias "GetWindow" _
-
(ByVal hWnd As Long, _
-
ByVal wCmd As Long) _
-
As Long
-
-
Private Const GW_CHILD = 5
-
Private Const GW_HWNDNEXT = 2
-
-
Private Declare Function apiGetWindowText Lib "user32" _
-
Alias "GetWindowTextA" _
-
(ByVal hWnd As Long, _
-
ByVal lpString As String, _
-
ByVal aint As Long) _
-
As Long
-
-
Private Declare Function apiSetActiveWindow Lib "user32" _
-
Alias "SetActiveWindow" _
-
(ByVal hWnd As Long) _
-
As Long
-
-
Private Declare Function apiIsIconic Lib "user32" _
-
Alias "IsIconic" _
-
(ByVal hWnd As Long) _
-
As Long
-
-
Private Declare Function apiShowWindowAsync Lib "user32" _
-
Alias "ShowWindowAsync" _
-
(ByVal hWnd As Long, _
-
ByVal nCmdShow As Long) _
-
As Long
-
-
Private Const SW_SHOW = 5
-
Private Const SW_RESTORE = 9
-
-
Public Function winGetClassName(hWnd As Long) As String
-
Dim sBuffer As String, iLen As Integer
-
sBuffer = String$(cMaxBuffer - 1, 0)
-
iLen = apiGetClassName(hWnd, sBuffer, cMaxBuffer)
-
If iLen > 0 Then
-
winGetClassName = Left$(sBuffer, iLen)
-
End If
-
End Function
-
-
Public Function winGetTitle(hWnd As Long) As String
-
Dim sBuffer As String, iLen As Integer
-
sBuffer = String$(cMaxBuffer - 1, 0)
-
iLen = apiGetWindowText(hWnd, sBuffer, cMaxBuffer)
-
If iLen > 0 Then
-
winGetTitle = Left$(sBuffer, iLen)
-
End If
-
End Function
-
-
Public Function winGetHWndDB(Optional hWndApp As Long) As Long
-
Dim hWnd As Long
-
winGetHWndDB = 0
-
If hWndApp <> 0 Then
-
If winGetClassName(hWndApp) <> "OMain" Then Exit Function
-
End If
-
hWnd = winGetHWndMDI(hWndApp)
-
If hWnd = 0 Then Exit Function
-
hWnd = apiGetWindow(hWnd, GW_CHILD)
-
Do Until hWnd = 0
-
If winGetClassName(hWnd) = "ODb" Then
-
winGetHWndDB = hWnd
-
Exit Do
-
End If
-
hWnd = apiGetWindow(hWnd, GW_HWNDNEXT)
-
Loop
-
End Function
-
-
Public Function winGetHWndMDI(Optional hWndApp As Long) As Long
-
Dim hWnd As Long
-
winGetHWndMDI = 0
-
If hWndApp = 0 Then hWndApp = Application.hWndAccessApp
-
hWnd = apiGetWindow(hWndApp, GW_CHILD)
-
Do Until hWnd = 0
-
If winGetClassName(hWnd) = "MDIClient" Then
-
winGetHWndMDI = hWnd
-
Exit Do
-
End If
-
hWnd = apiGetWindow(hWnd, GW_HWNDNEXT)
-
Loop
-
End Function
-
-
Public Function winCheckMultipleInstances(Optional fConfirm As Boolean = True) As Boolean
-
Dim fSwitch As Boolean, sMyCaption As String
-
Dim hWndApp As Long, hWndDb As Long
-
On Error GoTo ProcErr
-
sMyCaption = winGetTitle(winGetHWndDB())
-
hWndApp = apiGetWindow(apiGetDesktopWindow(), GW_CHILD)
-
Do Until hWndApp = 0
-
If hWndApp <> Application.hWndAccessApp Then
-
hWndDb = winGetHWndDB(hWndApp)
-
If hWndDb <> 0 Then
-
If sMyCaption = winGetTitle(hWndDb) Then Exit Do
-
End If
-
End If
-
hWndApp = apiGetWindow(hWndApp, GW_HWNDNEXT)
-
Loop
-
If hWndApp = 0 Then Exit Function
-
If fConfirm Then
-
If MsgBox(sMyCaption & " is already open@" _
-
& "Do you want to open a second instance of this database?@", _
-
vbYesNo Or vbQuestion Or vbDefaultButton2) = vbYes Then Exit Function
-
End If
-
apiSetActiveWindow hWndApp
-
If apiIsIconic(hWndApp) Then
-
apiShowWindowAsync hWndApp, SW_RESTORE
-
Else
-
apiShowWindowAsync hWndApp, SW_SHOW
-
End If
-
Application.Quit
-
ProcEnd:
-
Exit Function
-
ProcErr:
-
MsgBox Err.Description
-
Resume ProcEnd
-
End Function
-
'******************** Code End ********************
-
7 2923 NeoPa 32,556
Expert Mod 16PB
If the users have their own copy of the FE then it might be easier simply to check the size of the LDB file. Painless and reliable.
I did consider that, but I believe if the access application exits in error, that the .ldb file is left behind, and not cleaned up. How would you handle that?
@TheSmileyCoder:- I made a very subtle change in the Entry Level Function which really was not necessary.
- The Code works exactly as intended, as long as:
- You do not Pass a False Argument to the winCheckMultipleInstances() Function, as in:
- winCheckMultipleInstances(False)
- Your Code for winCheckMultipleInstances() is exactly as follows:
- Public Function winCheckMultipleInstances(Optional fConfirm As Boolean = True) As Boolean
-
Dim fSwitch As Boolean, sMyCaption As String
-
Dim hWndApp As Long, hWndDb As Long
-
-
On Error GoTo ProcErr
-
sMyCaption = winGetTitle(winGetHWndDB())
-
-
hWndApp = apiGetWindow(apiGetDesktopWindow(), GW_CHILD)
-
Do Until hWndApp = 0
-
If hWndApp <> Application.hWndAccessApp Then
-
hWndDb = winGetHWndDB(hWndApp)
-
If hWndDb <> 0 Then
-
If sMyCaption = winGetTitle(hWndDb) Then Exit Do
-
End If
-
End If
-
hWndApp = apiGetWindow(hWndApp, GW_HWNDNEXT)
-
Loop
-
-
If hWndApp = 0 Then Exit Function
-
-
If fConfirm Then
-
If MsgBox(sMyCaption & " is already open@" _
-
& "Do you want to open a second instance of this database?@", _
-
vbYesNo Or vbQuestion Or vbDefaultButton2) = vbYes Then Exit Function
-
-
apiSetActiveWindow hWndApp
-
-
If apiIsIconic(hWndApp) Then
-
apiShowWindowAsync hWndApp, SW_RESTORE
-
Else
-
apiShowWindowAsync hWndApp, SW_SHOW
-
End If
-
End If
-
-
Application.Quit
-
-
ProcEnd:
-
Exit Function
-
ProcErr:
-
MsgBox Err.Description
-
Resume ProcEnd
-
End Function
-
NeoPa 32,556
Expert Mod 16PB Smiley:
if the access application exits in error, that the .ldb file is left behind, and not cleaned up. How would you handle that?
Just as you would normally. Delete the file, then everything works normally again. You wouldn't expect everything to work perfectly when you had spurious files around.
Actually, it's a bonus, as it draws atention to the problem rather than leaving it there unnoticed until it mucks up something else - possibly in a way that's more subtle and hard to ascertain ;-)
PS. This seems to me to be a perfect illustration of the KISS concept.
@ adezii, Are you getting a non empty string value in sMyCaption? And what version of access are you using? I cant help but wonder if this is in someway related to me using Ac2010 and the code simply breaks in 2010.
@NeoPa How would you detect whether the a existing .ldb is the result of a bad exit (crash) or the result of the app allready being in use? I suppose I could try to kill it and catch the error if it is in use, but that also seems a bit crude, and file errors often seem to have poor performance/response times in my experience.
Thank you both for your time so far
NeoPa 32,556
Expert Mod 16PB
That question relies on knowing if multiple users have their own front-ends. If so, then the very existence of the file means that the user has it open already. If not, then it becomes more complicated and some parsing of the LDB file data may be required.
Are you getting a non empty string value in sMyCaption? And what version of access are you using? I cant help but wonder if this is in someway related to me using Ac2010 and the code simply breaks in 2010.
Sorry, but I forgot to mention that I am using Access 2002. I'm sending you my Demo Version for the Definitive Test. Open a 2nd Instance of the Demo, then click the Command Button - the Code should work fine.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Lauren Quantrell |
last post by:
I use this code in my Access 2K apps to check for multple instances of
my Access2K apps and it works fine on XP and WIn2K OS.
However, trying it on Access 2003/ XP OS it doesn't work and gets...
|
by: Mullin Yu |
last post by:
hi,
i have a web service that has file operations on Windows OS, and there may
be a file concurrency issue if only one working directory e.g. c:\working
therefore, i want to have a unique sub...
|
by: Martin Heuckeroth |
last post by:
Hi
We are working on a webservice application and are having some
problems with the cookies and/or sessions.
We have them working on our intranet but then its not working on the
internet. We...
|
by: tshad |
last post by:
I have been working with setting my drop boxes to allow double clicking to
select an item.
It worked fine until I made some changes. I then stripped the page down to
the bare essentials to find...
|
by: jojobar |
last post by:
Okay, I am trying to do is to test the webresource in 2.0
1. I created a new project with assembly name (and default assembly name)
"Office".
2. I added the following to the AssemblyInfo.cs...
|
by: Don |
last post by:
I'm having problems with intellisense, autocomplete, etc. suddenly not
working in certain classes of a project I'm working on. All the options are
set, and it all works fine for most classes, but...
|
by: MSDNAndi |
last post by:
Hi,
I have a set of simple webservices calls that worked fine using .NET
Framework 1.0.
I am calling a Java/Apache based webservices, the calling side is not able
to supply a proper WSDL.
...
|
by: qbproger |
last post by:
I'm developing a plugin for some software. The previous version of the
software didn't require a start in directory to be set. This allowed
me to leave the working directory to the default in the...
|
by: Jason Huang |
last post by:
Hi,
In our C# Windows Form application, we are using the SQL Server 2000 as the
database server.
The Database table MyTable has a field RegistrationDate which represents the
Date a client comes...
|
by: WORKING IN FAITH |
last post by:
three years I LOVE You Monica
More options
1 message - Collapse all
WORKING IN FAITH
View profile
More options Nov 13, 11:29 am
three years I LOVE You Monica
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |