473,320 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Check for Multiple Instance not working in Access 2003

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 stuck
in a loop (Do Until hWndApp = 0)

Any ideas?


Private Const cMaxBuffer = 255

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 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 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, myCaption As String, hWndApp As Long,
hWndDb As Long

myCaption = winGetTitle(winGetHWndDB())
hWndApp = apiGetWindow(apiGetDesktopWindow(), GW_CHILD)
Do Until hWndApp = 0
If hWndApp <> Application.hWndAccessApp Then
hWndDb = winGetHWndDB(hWndApp)
If hWndDb <> 0 Then
If myCaption = winGetTitle(hWndDb) Then Exit Do
End If
End If
hWndApp = apiGetWindow(hWndApp, GW_HWNDNEXT)
Loop
If hWndApp = 0 Then Exit Function

'leaving this code in allows the user the option of opening another
instance:
'*If fConfirm Then
'* If MsgBox(myCaption & " is already open" & vbCrLf & vbCrLf &
"" _
'* & "Do you want to open a second instance of this database?",
_
'* vbYesNo Or vbQuestion Or vbDefaultButton2) = vbYes Then Exit
Function
'*End If

If fConfirm Then
msgbox "You cannot open a second instance of this database."
End If

apiSetActiveWindow hWndApp

If apiIsIconic(hWndApp) Then
apiShowWindowAsync hWndApp, SW_RESTORE
Else
apiShowWindowAsync hWndApp, SW_SHOW
End If
DoCmd.Quit

End Function

Nov 13 '05 #1
0 1101

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Wolfgang Kaml | last post by:
Hello All, I have been working on this for almost a week now and I haven't anything up my sleeves anymore that I could test in addition or change.... Since I am not sure, if this is a Windows...
11
by: Ohaya | last post by:
Hi, I'm trying to understand a situation where ASP seems to be "blocking" of "queuing" requests. This is on a Win2K Advanced Server, with IIS5. I've seen some posts (e.g.,...
1
by: Jorge | last post by:
We have an application with Access 2000 and normally there are three designers working simultaneously. But forms or report modifications requires exclusive use. That force to all others users must...
0
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...
7
by: Dave | last post by:
Apologies for the newbie question. I have created a vb.net program for my company that is designed to work with Word Templates (about forty of them that we commonly use) that are selected by the...
3
by: mkjets | last post by:
I have worked for hours on trying to find a solution and have not figured it out. I am working in Access 2003. I need to create a query that takes values from 1 table and displays them in...
37
by: Allen Browne | last post by:
If you develop for others, you probably have multiple versions of Access installed so you can edit and create MDEs for clients in different versions. This works fine under Windows XP, even with...
6
by: Joseph Geretz | last post by:
I have the following class which I am serializing and passing back and forth between my Web Service application and the client. public class Token : SoapHeader { public string SID; public...
3
by: stevewilliams2004 | last post by:
I am attempting to create a singleton, and was wondering if someone could give me a sanity check on the design - does it accomplish my constraints, and/or am I over complicating things. My design...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.