Example Apps for Businesses, Schools & Developers

Page 1 Page 2

Version 1.1           Approx 0.6 MB (zipped)                 First Published 28 May 2024                 Last Updated 14 June 2024

This example app demonstrates how mouse down / mouse move events can be used to move the application window.
The idea was based on a suggestion by Spanish developer, Jesús Mansilla.

Initially, it seemed to me like a gimmick with no practical use. However, I have since found it to be very useful for various purposes.

For example, it can be useful for switching the currently visible application. I often use it to temporarily switch betweeen Access and PowerPoint during a presentation.


          MoveAppWindow_v1.1      (ACCDB - zipped)

Download and unblock the zip file. For more details, see my article: Unblock downloaded files by removing the Mark of the Web

Unzip and save the ACCDB file to a trusted location.

Using the Example App

The app opens to a startup form which explains its purpose.

Click the Open Standard Form button.

Hold down the left mouse button over the small button with the Access icon whilst you drag the form.
Both the application window and form will move in the direction specified.

Click the Open Popup Form button.

Again hold the left mouse button over the small Access button whilst you drag the form.
This time, the application window will move in the direction specified but the form will be left in its current position.

This happens because popup forms are independent of the application window.

In the second part of this article, I will provide code allowing users to drag both the popup form and application window at the same time e.g. onto another screen.

In this version, the code needed to do this on each form is identical and requires just two mouse events on each button:

Form Code:

Private Sub cmdMoveWindow_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
      'select application window
End Sub

' --------------------------------------
Private Sub cmdMoveWindow_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
      'drag entire application window
      If Button = 0 Then Exit Sub
End Sub

The underlying module code in modAppWindow is more complex. However, you do not need to understand it fully in order to use it!

Module Code:

Option Compare Database
Option Explicit


' Module modAppWindow

' DateTime : 17/02/2024
' Authors : Colin Riddington / Jesús Mansilla
' Website : https://www.isladogs.co.uk
' Purpose : Code to move the application window using mouse down / mouse move events
' Copyright : The code in the utility MAY be altered and reused in your own applications
' provided the copyright notice is left unchanged (including Author, Website and Copyright)
' You are NOT allowed to sell, resell or repost this on other sites such as online forums
' without permission from the author. However, links back to the above websites ARE allowed.
' If you find this code useful please place a link to my website on your own web site
' so that others may benefit as well.
' Updated : March 2024.


'APIs used for moving application window
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

Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

Public Declare PtrSafe Function GetWindowRect Lib "user32" (ByVal hwnd As LongPtr, lpRect As RECT) As Long

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

Public Type POINTAPI
      X As Long
      Y As Long
End Type

Const HWND_TOP = 0         'Moves MS Access window to top of Z-order
Const SWP_NOZORDER = &H4   'wFlags: Ignores the hWndInsertAfter
Const SWP_NOMOVE = &H2     'wFlags: don't change the window position
Const SWP_NOSIZE = &H1     'wFlags: don't change the window size

Dim AppX As Long, AppY As Long, AppTop As Long, AppLeft As Long, WinRECT As RECT, APointAPI As POINTAPI

Sub AppWindowSelect()

      'select application window
      GetWindowRect Application.hWndAccessApp, WinRECT
      AppTop = WinRECT.Top
      AppLeft = WinRECT.Left
      GetCursorPos APointAPI
      AppX = APointAPI.X
      AppY = APointAPI.Y

End Sub

Sub AppWindowMove()

      'drag application window
      GetCursorPos APointAPI
      SetWindowPos Application.hWndAccessApp, HWND_TOP, AppLeft - (AppX - APointAPI.X), _
           AppTop - (AppY - APointAPI.Y), 0, 0, SWP_NOZORDER + SWP_NOSIZE

End Sub


I have created a short video (3:44) to demonstrate the effect of doing this with both standard and popup forms

You can watch the Move the Application Window Using Code video on my Isladogs YouTube channel or you can click below:

If you liked the video, please subscribe to my Isladogs on Access channel on YouTube. Thanks.


Many thanks to Jesús Mansilla and Juanjo Luna for bringing this code to my attention.
I would be interested in any feedback on ways that other developers make use of it in their own applications.

Related Articles

Control the Application Interface        This shows how the Access application interface can be controlled e.g. hide the Access UI leaving forms 'floating on the desktop'

Move and Resize Borderless Form       This shows how borderless forms can be moved and resized using mouse events


Please use the contact form below to let me know whether you found this article interesting/useful or if you have any questions/comments.

Please also consider making a donation towards the costs of maintaining this website. Thank you

Colin Riddington           Mendip Data Systems                 Last Updated 14 June 2024

Return to Example Databases Page Page 1 of 2 1 2 Return To Top