Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get a forms position 1

Status
Not open for further replies.

Reinier11

Technical User
Jun 19, 2003
22
ES
Hi, a very simple question I assume, but can't find the answer.

In access forms don't have a property left or top. Is there another way to get the forms position?

Gr,
Reinier
 
You can use the API function GetWindowRect and some arithmetic, if you're comfortable with the Windows API. You'd have to calculate the offset from the Access application window, because GetWindowRect gives the position on the screen, not the position within the Access window.

Using the API takes you out of the safe zone VBA creates for you, so I wouldn't recommend this approach unless you're very comfortable with VBA programming in Access. You should also know how to use the Platform SDK documentation (it's in the MSDN Library), and preferably be using the Office Developer Edition.

Rick Sprague
Want the best answers? See faq181-2886
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
Aha, thanx for that. I'm not that familiar with using the Windows API, but I'll give it a go. I'll have some reading to do first, I suppose :)
 
If you've got Visual Studio or VB installed (it may even be in the Office Developer Edition), use the API Text Viewer to get the RECT structure, and the GetWindowRect function. Then read up on the function in the SDK, and you're good to go.

Rick Sprague
Want the best answers? See faq181-2886
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
Yep. r.tienkamp@sfl.nl
I haven't got Visual Studio or VB installed, not even in the Office Developer Edition. But I've found the SDK and the function.
Till now this is what i'm playiong around with:

Option Compare Database
Option Explicit

Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, _
lpRect As RECT) As Long

Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Const ERROR_INVALID_WINDOW_HANDLE As Long = 1400
Const ERROR_INVALID_WINDOW_HANDLE_DESCR As String = "Invalid window handle."

Sub PrintWindowCoordinates(hwnd As Long)
' Prints left, right, top, and bottom positions
' of a window in pixels.

Dim rectWindow As RECT

' Pass in window handle and empty the data structure.
' If function returns 0, an error occurred.
If GetWindowRect(hwnd, rectWindow) = 0 Then
' Check LastDLLError and display a dialog box if the error
' occurred because an invalid handle was passed.
If Err.LastDllError = ERROR_INVALID_WINDOW_HANDLE Then
MsgBox ERROR_INVALID_WINDOW_HANDLE_DESCR, _
Title:="Error!"
End If
Else
Debug.Print rectWindow.Bottom
Debug.Print rectWindow.Left
Debug.Print rectWindow.Right
Debug.Print rectWindow.Top
End If
End Sub

The thing is: the values don't change with the forms position, so i think i only have it's size. What am I overlooking?

 
I don't see anything wrong, unless you're not passing a correct window handle. Use hWndAccessApp for the Access application window. Use frm.hWnd for a Form window.

I experimented with this code, which I clipped from a working app, and it definitely returned the coordinates of either the Access window or the form, relative to the desktop. See if this works any differently for you.
Code:
Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

Private Declare Function GetWindowRect Lib "user32" _
    (ByVal hwnd As Long, lpRect As RECT) As Long

Public Sub GetWindowPos(Optional frm As Form)
    Dim WinRect As RECT
    
    If frm Is Nothing Then
        GetWindowRect hWndAccessApp, WinRect
    Else
        GetWindowRect frm.hwnd, WinRect
    End If
    With WinRect
        Debug.Print "Left="; CStr(.Left); ", Top="; CStr(.Top); _
            ", Right="; CStr(.Right); ", Bottom="; CStr(.Bottom)
    End With
End Sub

Rick Sprague
Want the best answers? See faq181-2886
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
Thanks. Nothing wrong with the code indeed. Used the right handles, but forgot to pass them though, my mistake.

How to reward you?
 
You're welcome. Coming back to say "thanks, that worked" is always appreciated.

There is also the "star" (a.k.a. Tipmaster vote) system, which helps other people find solutions. You vote by clicking the "Mark this post..." link at the left under the post. (Mind you, I'm not begging for a star--I'm answering your question about rewards.)

Rick Sprague
Want the best answers? See faq181-2886
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top