PDA

Click to See Complete Forum and Search --> : Stay on top...


jlegan
Jan 16th, 2001, 04:56 PM
Ok, database is 99.9% complete. One problem, kinda a big one, I need to get the form to stay on top at all times.

When the form opens the access window disappears, when it does it opens in the bottom right hand corner of the screen, no bigger than the banner at the top of this website.

What I need to do now is keep that window on top at all times. People have pointed out code for this at http://www.vbsquare.com/tips/tip153.html and the vb code is there but I cannot for the life of me get this to work with access.

Could someone please help, this is the only thing holding me up in finishing the project.

Thanks in advance,

Jim

Chris
Jan 17th, 2001, 03:04 AM
Juz call the SetWindowPos API will do.


Option Explicit
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 Const HWND_NOTOPMOST = -2
Private Const HWND_TOPMOST = -1
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1

Private Sub Form_Load()
'Set TopMost
SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
'Reset TopMost
SetWindowPos Me.hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
End Sub

jlegan
Jan 17th, 2001, 09:25 AM
I get the following error message when trying to open the form when I put in the code above.

----------------------------
Compile Error:
Sub or Function not defined.
----------------------------

Now to make sure I am not doing this wrong.

I put this in a "module" in the database:
*******************************************
Option Explicit
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 Const HWND_NOTOPMOST = -2
Private Const HWND_TOPMOST = -1
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1
*******************************************

And I put this in the OnLoad Event Procedure in the form:
*******************************************
Private Sub Form_Load()
'Set TopMost
SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
End Sub
*******************************************

Am I doing this right and it is just not working or am I messing this up somewhere?

Chris
Jan 17th, 2001, 09:40 AM
jlegan, If you put all the API declaration under a Basic Module filr then you should declare it as Public.

'Coz if you declare as Private, that mean it only visible within the Basic Module file only.

If you insist to declare as Private, then you need to declare it under the respective Form.


Option Explicit
Public 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
Public Const HWND_NOTOPMOST = -2
Public Const HWND_TOPMOST = -1
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1

jlegan
Jan 17th, 2001, 09:52 AM
Ok,

It no longer errors out however it does not stay on top.

To verify, when I click on another application on the task
bar I need this small form to remain on top. The other
programs still have the focus when you are working in them
but the form never leaves your sight, always on top. Will the code you provide do that?

Chris
Jan 17th, 2001, 09:58 AM
:eek: I juz try on my PC, it does stay on top of others windows. Perhap you can post your code?

jlegan
Jan 17th, 2001, 10:14 AM
Ok, there are three things that this form does (code wise). They are listed below with a brief explanation above them and split into two parts, whats in the module and whats in the "On" Event in the form itself.

#1 Minimize Access Window: This code minimizes the Access window so only the form displays on the desktop:

Module:
-------------------------------------
Option Compare Database
Option Explicit

Global Const SW_HIDE = 0
Global Const SW_SHOWNORMAL = 1
Global Const SW_SHOWMINIMIZED = 2
Global Const SW_SHOWMAXIMIZED = 3


Private Declare Function apiShowWindow Lib "user32" _
Alias "ShowWindow" (ByVal hwnd As Long, _
ByVal nCmdShow As Long) As Long

Function fSetAccessWindow(nCmdShow As Long)
'Usage Examples
'Maximize window:
' ?fSetAccessWindow(SW_SHOWMAXIMIZED)
'Minimize window:
' ?fSetAccessWindow (SW_SHOWMINIMIZED)
'Hide window:
' ?fSetAccessWindow (SW_HIDE)
'Normal window:
' ?fSetAccessWindow(SW_SHOWNORMAL)
'
Dim loX As Long
Dim loForm As Form
On Error Resume Next
Set loForm = Screen.ActiveForm
If Err <> 0 Then 'no Activeform
If nCmdShow = SW_HIDE Then
MsgBox "Cannot hide Access unless " _
& "a form is on screen"
Else
loX = apiShowWindow(hWndAccessApp, nCmdShow)
Err.Clear
End If
Else
If nCmdShow = SW_SHOWMINIMIZED And loForm.Modal = True Then
MsgBox "Cannot minimize Access with " _
& (loForm.Caption + " ") _
& "form on screen"
ElseIf nCmdShow = SW_HIDE And loForm.PopUp <> True Then
MsgBox "Cannot hide Access with " _
& (loForm.Caption + " ") _
& "form on screen"
Else
loX = apiShowWindow(hWndAccessApp, nCmdShow)
End If
End If
fSetAccessWindow = (loX <> 0)
End Function

*******************************
OnOpen Event in form:
-------------------------------
Private Sub Form_Open(Cancel As Integer)
fSetAccessWindow (SW_SHOWMINIMIZED)
End Sub

#2 Specify location on screen: This tells access what size the form should be and where it should be located on the screen (set for bottom right hand corner at 1024x768 right now.

In Module:
-------------------------
Option Compare Database
Option Explicit

Declare Function MoveWindow Lib "user32" _
(ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth _
As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
**********************
OnLoad Event in form:
----------------------
Private Sub Form_Load()
Call MoveWindow(Me.hwnd, 630, 630, 400, 100, 1)
End Sub

#3 is the one you just help me setup and the code is listed above.

I think where the difference in our setups and why mine is not working is that the access window is minimized to the task bar on mine and the only visible thing from access is the small form itself. Maybe what I want to do cannot be done. Let me know what your outcome is.

Jim

Also if you need me to send you the dBase itself let me know it is very small right now.

Chris
Jan 17th, 2001, 10:27 AM
jlegan, few question here.

1. Which function you'll call first, second and third?


Private Sub Form_Open(Cancel As Integer)
fSetAccessWindow (SW_SHOWMINIMIZED)
End Sub

'OR

Private Sub Form_Load()
Call MoveWindow(Me.hwnd, 630, 630, 400, 100, 1)
End Sub

'OR the one I posted



2. It your application is MS Access?

jlegan
Jan 17th, 2001, 10:31 AM
Yes this is completely in MS Access this is a database mbd file and I am doing this all (to my knowledge) in VBA.

I also have no idea in Access what order they launch in. I commented all the other ones out and just tried yours and it failed so I dont know if that will effect it.

Chris
Jan 17th, 2001, 10:45 AM
Err... I'm not good in VBA. but I'll try my best to help you too.

Since you have minimized the MSAccess why not you juz put the code under the Form_Resize event?


Private Sub Form_Resize()
If Me.WindowState <> vbMinimized Then
'Reposition the window
MoveWindow Me.hwnd, 630, 630, 400, 100, True

'Set TopMost
SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
End If
End Sub

jlegan
Jan 17th, 2001, 11:26 AM
I hope you dont mind but I just sent a copy of the dBase to
your yahoo acct (about 100k). I tried to impliment the code
but it errored out (after commenting out the other code. If
you cant help with it no big deal I appreciate everything you have done so far.

Jim

Chris
Jan 17th, 2001, 11:31 AM
ok. jlegan