Bring form to front in windows

Batraul

Registered User.
Local time
Today, 08:28
Joined
Dec 15, 2014
Messages
13
Hi all!

I've looked for how to do but everywhere people asks how to bring a form in the front of all forms.
I have a form emergent and modal. The program hides the access enviroment.
But, when the form is loaded, it always appears under the window that I have active (whatever the window is, an excel sheet, internet explorer, etc).

Is there any vba command to bring the form (or maybe the access application) to the front as the first visible window?

Thank you in advance for the help.

Regards.
 
This is the code to put into the form that needs to be visible. Before you do anything perational - test this in a simple separate form.

Code:
Option Compare Database
Option Explicit

#If VBA7 Then

Private Declare PtrSafe Function SetWindowPos Lib "user32" (ByVal hwnd As LongPtr, _
                                                    ByVal hWndInsertAfter As LongPtr, ByVal x As Long, ByVal y As Long, ByVal _
                                                                                                                     cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare PtrSafe Function IsWindowVisible Lib "user32" (ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function ShowWindow Lib "user32" (ByVal hwnd As LongPtr, _
                                                  ByVal nCmdShow As Long) As Long
                                                  
#Else
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
Private Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, _
                                                  ByVal nCmdShow As Long) As Long
#End If
                                                  
                                                  
Const SW_HIDE = 0
Private Const SW_SHOW = 5
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1
Private Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2

Private Sub cmdAlwaysOnTop_Click()
    If IsWindowVisible(Application.hWndAccessApp) Then
        ShowWindow Application.hWndAccessApp, SW_HIDE
        SetWindowPos Application.hWndAccessApp, HWND_TOPMOST, 0, 0, 0, 0, _
                     SWP_NOMOVE Or SWP_NOSIZE
        ShowWindow Me.hwnd, SW_SHOW    'Not needed in Access 97
        Me.Repaint    'Not needed in Access 97
    Else
        ShowWindow Application.hWndAccessApp, SW_SHOW
        SetWindowPos Application.hWndAccessApp, HWND_NOTOPMOST, 0, 0, 0, 0, _
                     SWP_NOMOVE Or SWP_NOSIZE
    End If
End Sub

Private Sub Form_Current()
    cmdAlwaysOnTop_Click
End Sub

Private Sub Form_Unload(Cancel As Integer)
    If IsWindowVisible(Application.hWndAccessApp) = False Then
        ShowWindow Application.hWndAccessApp, SW_SHOW
        SetWindowPos Application.hWndAccessApp, HWND_NOTOPMOST, 0, 0, 0, 0, _
                     SWP_NOMOVE Or SWP_NOSIZE
    End If
End Sub
For testing, you can execute cmdAlwaysOnTop_Click() from a button
 
Dear Spikepl,

Thank you very much for your reply.
It worked perfectly!!

Thank you again.
 
This is the code to put into the form that needs to be visible. Before you do anything perational - test this in a simple separate form.

Code:
Option Compare Database
Option Explicit

#If VBA7 Then

Private Declare PtrSafe Function SetWindowPos Lib "user32" (ByVal hwnd As LongPtr, _
                                                    ByVal hWndInsertAfter As LongPtr, ByVal x As Long, ByVal y As Long, ByVal _
                                                                                                                     cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare PtrSafe Function IsWindowVisible Lib "user32" (ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function ShowWindow Lib "user32" (ByVal hwnd As LongPtr, _
                                                  ByVal nCmdShow As Long) As Long
                                                 
#Else
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
Private Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, _
                                                  ByVal nCmdShow As Long) As Long
#End If
                                                 
                                                 
Const SW_HIDE = 0
Private Const SW_SHOW = 5
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1
Private Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2

Private Sub cmdAlwaysOnTop_Click()
    If IsWindowVisible(Application.hWndAccessApp) Then
        ShowWindow Application.hWndAccessApp, SW_HIDE
        SetWindowPos Application.hWndAccessApp, HWND_TOPMOST, 0, 0, 0, 0, _
                     SWP_NOMOVE Or SWP_NOSIZE
        ShowWindow Me.hwnd, SW_SHOW    'Not needed in Access 97
        Me.Repaint    'Not needed in Access 97
    Else
        ShowWindow Application.hWndAccessApp, SW_SHOW
        SetWindowPos Application.hWndAccessApp, HWND_NOTOPMOST, 0, 0, 0, 0, _
                     SWP_NOMOVE Or SWP_NOSIZE
    End If
End Sub

Private Sub Form_Current()
    cmdAlwaysOnTop_Click
End Sub

Private Sub Form_Unload(Cancel As Integer)
    If IsWindowVisible(Application.hWndAccessApp) = False Then
        ShowWindow Application.hWndAccessApp, SW_SHOW
        SetWindowPos Application.hWndAccessApp, HWND_NOTOPMOST, 0, 0, 0, 0, _
                     SWP_NOMOVE Or SWP_NOSIZE
    End If
End Sub
For testing, you can execute cmdAlwaysOnTop_Click() from a button
First great code! Finally managed to show form above all open apps on desktop
But I have still a problem ,,
After running cmdAlwaysOnTop_Click, all minimized apps will not open when pressing their command bar icons ,,
Any Idea?
 

Users who are viewing this thread

Back
Top Bottom