Disable/Hide Minimum/Maximum/Close Items On the Access Runtime Application Window

S

Stewart Berman

I need to be able to either disable or hide the Minimum/Maximum/Close on the Access Runtime
application window. Those are the items in the top left corner of the Access application window not
the ones on a form.

I have tried using the Get/SetWindowLong API calls to get the current Window Style, turn off the
bits associated with the Minimum/Maximum/Close and then set the Window Style to the new value. This
only works for the minimize button. The other two, maximize and close are unaffected no matter what
bits I turn off.

I am using Application.hWndAccessApp as the window handle for the Get/SetWindowLong API calls.

How do I get around this?
 
J

Jeanette Cunningham

Hi Stewart,
here is code to disable the main app close button.
Put the code in a new module (not in a code module behind a form).

Save it with a name like modDisableBtn
-----------------------------------------
Call it like this on your start up form or autoexec macro.

Dim C As clsAppCloseButton
Set C = New clsAppCloseButton

'Enable Close menu.
C.Enabled = False
------------------------------

code below goes in general module
-------------------------
Option Compare Database
Option Explicit



Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, _
ByVal bRevert As Long) As Long

Private Declare Function EnableMenuItem Lib "user32" (ByVal hMenu As _
Long, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long

Private Declare Function GetMenuItemInfo Lib "user32" Alias _
"GetMenuItemInfoA" (ByVal hMenu As Long, ByVal un As Long, ByVal B As _
Long, lpMenuItemInfo As MENUITEMINFO) As Long

Private Type MENUITEMINFO
cbSize As Long
fMask As Long
fType As Long
fState As Long
wID As Long
hSubMenu As Long
hbmpChecked As Long
hbmpUnchecked As Long
dwItemData As Long
dwTypeData As String
cch As Long
End Type



Public Property Get Enabled() As Boolean
On Error GoTo Err_Handler
pstrProc = "Enabled"

Dim hwnd As Long
Dim hMenu As Long
Dim result As Long
Dim MI As MENUITEMINFO

MI.cbSize = Len(MI)
MI.dwTypeData = String(80, 0)
MI.cch = Len(MI.dwTypeData)
MI.fMask = MF_GRAYED
MI.wID = SC_CLOSE
hwnd = Application.hWndAccessApp
hMenu = GetSystemMenu(hwnd, 0)
result = GetMenuItemInfo(hMenu, MI.wID, 0, MI)
Enabled = (MI.fState And MF_GRAYED) = 0

Exit_Handler:
Exit Property
Err_Handler:
Call fnFormErrHandler(pstrProc, pstrMdl, Err)
Resume Exit_Handler

End Property

Public Property Let Enabled(boolClose As Boolean)
On Error GoTo Err_Handler
pstrProc = "Enabled"

Dim hwnd As Long
Dim wFlags As Long
Dim hMenu As Long
Dim result As Long

hwnd = Application.hWndAccessApp
hMenu = GetSystemMenu(hwnd, 0)
If Not boolClose Then
wFlags = MF_BYCOMMAND Or MF_GRAYED
Else
wFlags = MF_BYCOMMAND And Not MF_GRAYED
End If
result = EnableMenuItem(hMenu, SC_CLOSE, wFlags)

Exit_Handler:
Exit Property
Err_Handler:
Call fnFormErrHandler(pstrProc, pstrMdl, Err)
Resume Exit_Handler

End Property
--------------------------


I did have code to disable the min and max buttons, but lost it when I found
that it was very inconvenient for users if they were disabled.


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
 
J

Jeanette Cunningham

It works for me in A2007.
This bit that calls the function

Dim C As clsAppCloseButton
Set C = New clsAppCloseButton

'Enable Close menu.
C.Enabled = False

needs to be part of a sub in your start up functions.

Where have you put the above code in your app?


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
 
S

Stewart Berman

I put this in a module (the class name was changed):
Public Function Main()
Set CLog = New c_Log
Dim CApplicationButtions As c_ApplicationButtons
Set CApplicationButtions = New c_ApplicationButtons
CApplicationButtions.Enabled = False
End Function
and called it from an AutoExec macro.

This is the code from the Property Set Enable (your error handling code was commented out and
logging added):
Public Property Let Enabled(boolClose As Boolean)
On Error GoTo Err_Handler
'pstrProc = "Enabled"

Dim hwnd As Long
Dim wFlags As Long
Dim hMenu As Long
Dim result As Long

hwnd = Application.hWndAccessApp
hMenu = GetSystemMenu(hwnd, 0)
If Not boolClose Then
wFlags = MF_BYCOMMAND Or MF_GRAYED
Else
wFlags = MF_BYCOMMAND And Not MF_GRAYED
End If
CLog.Log "wFlags: " & Hex$(wFlags)
result = EnableMenuItem(hMenu, SC_CLOSE, wFlags)
CLog.Log "result: " & Hex$(result)

Exit_Handler:
Exit Property
Err_Handler:
'Call fnFormErrHandler(pstrProc, pstrMdl, Err)
Resume Exit_Handler

End Property

The log contained:
ApplicationButtons Initialized
wFlags: 1
result: FFFFFFFF

But the close button was still enabled. I tried this in a regular .accdb and in a .accde using the
/RunTime option. Neither one worked using MS Access 2007.

I originally tried it without the logging. It was added to verify what was happening.
 
J

Jeanette Cunningham

I saved a production database as a runtime app by changing its file
extension to .accdr.
I tested opening the database and I can confirm that the main app close
button was disabled.

Here is the skeleton of the code that opens this app.

I removed a couple of functions calls that save the users settings, sets the
app title and checks versions.
I have not included the function that checks if the database is in user mode
or developer mode.
I do not use an autoexec macro - but a startup form instead.
The code below runs from the load event of the startup form.
The startup form is set in the options settings for the database.


Private Sub Form_Load()
'checks the links to the backend and if OK
'checks if versions match then
'opens the database
'otherwise closes the database
'other stuff, options, main app buttons
'modified 11/5/09
On Error GoTo Err_Handler

If CheckAndRelink = True Then
'keep a persistent connection to the backend open all the time the
db is open
'fhdnKeepOpen is bound to tblKeepOpen
DoCmd.OpenForm "fhdnKeepOpen", , , , , acHidden

If UserMode = True Then
Call DisableAppCloseBtn
End If

'open the splash screen which displays app info and opens the main
menu
Call InitSplash
DoCmd.Close acForm, "frmStartup"
End If

Exit_Handler:
DoCmd.Hourglass False
Exit Sub

Err_Handler:
'got a failure, tell user
MsgBox "Unexpected error during startup"
DoCmd.Close acForm, "frmStartup"
Application.Quit acQuitSaveNone
Resume Exit_Handler

End Sub

Private Function DisableAppCloseBtn()
On Error GoTo Err_Handler

Dim C As clsAppCloseButton
Set C = New clsAppCloseButton

C.Enabled = False

Exit_Handler:
Exit Function
Err_Handler:
MsgBox Err.Number
Resume Exit_Handler
End Function



Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
 
S

Stewart Berman

Would you believe the problem was that I didn't have the SC_ and MF_ constants defined as long (i.e.
I had &H1 instead of &H&)?
 
C

Charles Wang [MSFT]

Hi Stewart,
Whether or not defining the constants SC_ and MF_ is not necessary if you
directly specify the value instead. May you let us know what the result is
for Jeanette's suggestion?

Best regards,
Charles Wang
Microsoft Online Community Support
=========================================================
Delighting our customers is our #1 priority. We welcome your
comments and suggestions about how we can improve the
support we provide to you. Please feel free to let my manager
know what you think of the level of service provided. You can
send feedback directly to my manager at: (e-mail address removed).
=========================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
=========================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top