MS Access – VBA – Minimize/Maximize Access Application

Here is another common question, how can I control the Application window itself? Well that depends on what exactly you wish to do. So things are easy to do and other require APIs, etc.
 

Minimize/Maximize and Restore the MS Access Application Window

One can very easily control the Application Window state with one simple command, the often overlooked DoCmd.RunCommand!

DoCmd.RunCommand acCmdAppMinimize 'Minimize the MS Access Application
DoCmd.RunCommand acCmdAppMaximize 'Maximize the MS Access Application
DoCmd.RunCommand acCmdAppRestore 'Restore the MS Access Application

 

Completely Hide the MS Access Application Window

Once again, a not so uncommon question. Now implementing it requires a little more programming than merely minimizing or maximizing the application window, but it can be done! Now if you Google the subject you will find any number of code samples. That said, before I ‘waste‘ my time searching for anything relating to MS Access I always go and check The Access Web where you will find a ready to use API entitled Manipulate Access Window to do exactly this.

5 responses on “MS Access – VBA – Minimize/Maximize Access Application

  1. Stephen Batich

    Private Declare Function GetSystemMenu Lib “user32″ (ByVal hwnd As Long, ByVal wRevert As Long) As Long
    Private Declare Function EnableMenuItem Lib “user32″ (ByVal hMenu As Long, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long

    Public Sub AccessCloseButtonEnabled(pfEnabled As Boolean)
    ‘ Comments: Control the Access close button.
    ‘ Disabling it forces the user to exit within the application
    ‘ Params : pfEnabled TRUE enables the close button, FALSE disabled it

    On Error Resume Next

    Const clngMF_ByCommand As Long = &H0&
    Const clngMF_Grayed As Long = &H1&
    Const clngSC_Close As Long = &HF060&

    Dim lngWindow As Long
    Dim lngMenu As Long
    Dim lngFlags As Long

    lngWindow = Application.hWndAccessApp
    lngMenu = GetSystemMenu(lngWindow, 0)
    If pfEnabled Then
    lngFlags = clngMF_ByCommand And Not clngMF_Grayed
    Else
    lngFlags = clngMF_ByCommand Or clngMF_Grayed
    End If
    Call EnableMenuItem(lngMenu, clngSC_Close, lngFlags)
    End Sub

  2. De King

    Thanks a ton for this gem of support – I’ve been searching all day for this info… WITH CONCISE REFERENCES !

  3. Mat

    Excuse my ignorance here but I attempt to use this and it seems to need an additional assembly. The Function Declaration is Red. Just trying to identify the references it needs – any ideas?

    1. Daniel Pineault Post author

      I just tried Stephen’s code and got several errors, but they were related to the quotes within the code (some text conversion issue it would seem). Check all the single and double quotes (replace them all) and it should work fine. Below was my cleaned up version of the same code (I don’t know if the same issue will occur with me pasting it).

      Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal wRevert As Long) As Long
      Private Declare Function EnableMenuItem Lib "user32" (ByVal hMenu As Long, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long
      
      Public Sub AccessCloseButtonEnabled(pfEnabled As Boolean)
      ' Comments: Control the Access close button.
      ' Disabling it forces the user to exit within the application
      ' Params : pfEnabled TRUE enables the close button, FALSE disabled it
          On Error Resume Next
      
          Const clngMF_ByCommand    As Long = &H0&
          Const clngMF_Grayed       As Long = &H1&
          Const clngSC_Close        As Long = &HF060&
      
          Dim lngWindow             As Long
          Dim lngMenu               As Long
          Dim lngFlags              As Long
      
          lngWindow = Application.hWndAccessApp
          lngMenu = GetSystemMenu(lngWindow, 0)
          If pfEnabled Then
              lngFlags = clngMF_ByCommand And Not clngMF_Grayed
          Else
              lngFlags = clngMF_ByCommand Or clngMF_Grayed
          End If
          Call EnableMenuItem(lngMenu, clngSC_Close, lngFlags)
      End Sub