• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

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

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!

  • 2
1 Solution
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
'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

CynthiaZCarAuthor Commented:
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.
CynthiaZCarAuthor Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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