How do I bring the focus back to a form while the program is running - VB6?

Posted on 2009-04-29
Last Modified: 2012-06-27
Currently, we have a 3rd party app (written in VB6) that loses focus while it's running in the middle of the night.  When it loses focus, processing stops.  

I have a small VB6 app that runs throughout the night, checking for error dialogs using the FindWindow function.  When my app finds an error dialog by its caption/title and classname (#32770), and gets the handle to the dialog box, I use   SendMessage handle, WM_CLOSE, 1, 0 to close the error dialog.  That process works great.

My problem is that now we have the 3rd party app losing focus on a small form - and again, processing stops.  I have tried using FindWindow to get the handle of the small form - but with no success.  While I am searching for the caption that is on the form at the EXACT time of the lost focus - it turns out that the form caption is actually Form1 when it's compiled.  During processing, the caption for Form1 is changed on the fly.

I need a way to find the handle of the form in VB6, so I can send a command to it to bring focus back to it.  I need to be able to add this to my small VB6 app that runs during the night - we can't make changes to the 3rd party app (of course).

Just as an FYI, if I remote into the server while this 3rd party app has lost focus on the small form - all I have to do is click the mouse on the lost focus form - and the application takes off running.

I'd really appreciate some expert help here.  Thanks!

Question by:CynthiaZCar
    LVL 1

    Expert Comment

    Do you know the hwnd of the form?  If so, you could call the function below to set it to always be on top...  This may need to be set up in a timer event.  Be careful with this since it can cause message boxes needing attention to shop up behind the form you want to be on top.

    Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
    Const SWP_NOSIZE = &H1
    Const SWP_NOMOVE = &H2
    Const SWP_SHOWWINDOW = &H40
    Const HWND_NOTOPMOST = -2
    Const HWND_TOPMOST = -1
    'Set a form always on the top.
    'The form can be specified as a Form or object or through its hWnd property.
    'If OnTop=False the always on the top mode is de-activated.
    Sub SetAlwaysOnTopMode(hWndOrForm As Variant, Optional ByVal OnTop As Boolean = True)
        Dim hWnd As Long
        ' get the hWnd of the form to be move on top
        If VarType(hWndOrForm) = vbLong Then
            hWnd = hWndOrForm
            hWnd = hWndOrForm.hWnd
        End If
    End Sub

    Open in new window


    Author Comment

    Thank you for your help - however, I do not know the hWnd of the form.  I need a way to find the handle of the form in VB6, so I can send a command to it to bring focus back to it.  I have tried

    handle = FindWindow(vbNullString, "Caption of the form")

    This does not work for that particular form.  This form is out of focus.  Please advise more again.  Thanks.

    Accepted Solution

    I was able to solve my own problem by running my VB6 application as the SAME user the 3rd party application was running as.  It found the correct handle, brought focus back to the form and continued along.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
    Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now