I find this to be more versatile than App.PrevInstance.
IE: not only will it check for previous running instances but will bring the app to the foreground if it had been minimized.
You can easily modify it to do other stuff besides just that.
Please compile before running
Cheers!
VB Code:
'Form code
Option Explicit
Private Sub Form_Load()
Call CheckAppPrevInstance
End Sub
'In a module
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, _
ByVal wCmd As Long) As Long
Private Declare Function OpenIcon Lib "user32" (ByVal hWnd As Long) As Long
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
Private Const GW_HWNDPREV = 3
Public Sub CheckAppPrevInstance()
Dim OldTitle As String
Dim ll_WindowHandle As Long
'saving the current title in OldTitle variable
'and changing the application title so it will not look for itself
OldTitle = App.Title
App.Title = "Dup instance"
'finding the previous instance.
ll_WindowHandle = FindWindow("ThunderRT6Main", OldTitle)
'if there is no old instances of your application - exit.
If ll_WindowHandle = 0 Then App.Title = OldTitle: 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)
End
End Sub