Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 592
  • Last Modified:

Switching to app after app.previnstance is called

Hi, I am sure this is fairly easy, but I want to be able to allow my application to switch to the running one when it is loaded again, rather than just displaying a message box with "You can't do that!" I would like to show the application without an error. I currently have this code:

    If App.PrevInstance Then
        MsgBox "AutoBill is already running, you can only run one version of AutoBill at a time", vbInformation, App.Title
        Exit Sub
    End If

I want to switch to autobill.
0
duckbert
Asked:
duckbert
  • 2
  • 2
  • 2
1 Solution
 
anand2kCommented:
hi duckbert


U can use AppActivate statement
just pass the title of the application u want to activate to the appactivate statement

e.g.

AppActivate "AutoBill"


Bye
0
 
Éric MoreauSenior .Net ConsultantCommented:
This is what I use:

Option Explicit

Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Declare Function SetFocus Lib "user32" (ByVal hwnd As Long) As Long
Declare Function SetActiveWindow Lib "user32" (ByVal hwnd As Long) As Long
Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Public Const SW_SHOW = 5

Public Sub Main()
Dim strTemp As String
Dim lngHandle As Long


    If App.PrevInstance Then
        MsgBox "Application déjà loadée!!!"
        lngHandle = FindWindow(strTemp, "Test PrevInstance")
        Call ShowWindow(lngHandle, SW_SHOW)
        Call SetFocus(lngHandle)
        Call SetActiveWindow(lngHandle)
        Call SetForegroundWindow(lngHandle)
        End
    Else
        frmPrevInstance.Show
    End If
End Sub
0
 
duckbertAuthor Commented:
I am still a little stumped about why this isn't working.

I entered this as my code

    If App.PrevInstance Then

       AppActivate App.Title

       End

    End If

With all my windows having the variable set to app.title

I tried to get both suggestions to work, but I couldn't, I also checked MSDN for app.activate and came up with what I was supplied with. Why isn't this working? and I can't seem to get it to work through any shell idea either. Maybe if I could find the process id of my previous app that could work but I still don't know what to do.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
anand2kCommented:
hi dukbert

u add the procedure named 'Main' in a module and make the startup of project as 'Sub Main' in project properties.

a sample proc should look like this....


Public Sub Main()
If App.PrevInstance Then
    MsgBox "prev instance exist"
    AppActivate "anand"
    End
Else
    Form1.Show        ' u startup object
End If
End Sub

one thing u pls note , here "anand" means the title of ur application, i m doubfule 4 app.title. u make some hard coded title 4  ur application.



hope this works

bye anand
0
 
Éric MoreauSenior .Net ConsultantCommented:
be sure that the handle (lngHandle) returned from FindWindow is not 0. If it is 0, this means that the title you pass to the function is not correct.
0
 
duckbertAuthor Commented:
Still had a few problems with this answer, and for some reason it wouldn't work when I first tried it out. Anyway, I added something else and a few versions later it magically worked with this method. So sorry for taking so long to award the points
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now