?
Solved

Switching to app after app.previnstance is called

Posted on 2003-03-07
6
Medium Priority
?
575 Views
Last Modified: 2012-08-14
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
Comment
Question by:duckbert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 1

Accepted Solution

by:
anand2k earned 120 total points
ID: 8093389
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
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 8093883
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
 
LVL 1

Author Comment

by:duckbert
ID: 8096942
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Expert Comment

by:anand2k
ID: 8097220
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
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 8097533
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
 
LVL 1

Author Comment

by:duckbert
ID: 8164519
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

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month12 days, 18 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question