How do i keep a user from opening more than one of the same vb application? I want only one to be able to be run at a time, unlike msword that can run multiple times.
Printable View
How do i keep a user from opening more than one of the same vb application? I want only one to be able to be run at a time, unlike msword that can run multiple times.
If App.PrevInstance = True Then
Unload Me
End If
Hi
I got this code on this forum somewhere. It is better than using the App.Previnstance / unload method because it actually jumps to the currently running instance of ur project.
Regards
Stuart
VB Code:
Declare Function FindWindow Lib "user32" Alias "FindWindowA" _ (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, _ ByVal wCmd As Long) As Long Declare Function OpenIcon Lib "user32" (ByVal hwnd As Long) As Long Declare Function SetForegroundWindow Lib "user32" (ByVal _ hwnd As Long) As Long Public Const GW_HWNDPREV = 3 Public Sub ShowPrevInstance() On Error Resume Next Dim OldTitle As String Dim ll_WindowHandle As Long 'saving the current title in OldTitle variable and changing the application title OldTitle = App.Title App.Title = "New App - This App Will Be Closed" 'finding the previous instance. if you are using VB 5.0 change "ThunderRT6Main" to "ThunderRT5Main" ll_WindowHandle = FindWindow("ThunderRT6Main", OldTitle) 'if there is no old instances of your application - exit. If ll_WindowHandle = 0 Then Exit Sub 'Find the window we need to restore ll_WindowHandle = GetWindow(ll_WindowHandle, GW_HWNDPREV) 'Now restore it Call OpenIcon(ll_WindowHandle) 'And Bring it to the foreground Call SetForegroundWindow(ll_WindowHandle) Unload Me Set [Your Form Name Here] = Nothing End Sub
thanks
On the last line does the title go inside [] or did you just put those there as markers? show me an example just to make sure i understand.
Hi
Just insert the name of the form that loads at start up in ur app
So, if it is form1 do...
Set Form1 = Nothing
You dont even need this line in all cases but does no harm
Regards
Stuart
got ya
Ok the code works in the case that it willnot allow another application to open but it does not popup the running application do you know why?
Hi again
It works but i hope that you actually called the routine...
In ur Sub Main or Form Load or wherever your project starts u need to call the routine
RegardsVB Code:
If App.PrevInstance Then ShowPrevInstance
Stuart
Thanks that code works great
I don't know about VB6, but VB5's class window name is "ThunderRT5Form", NOT "ThunderRT5Main".
NOTE: You can find the classname of any window using "Find window" option in SPY++ (bundled with VC++). It's very easy and it's helped me out alot recently. :)
::Sibby::