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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disable the Access 2007 application close button

Status
Not open for further replies.

Artois27

Technical User
Nov 19, 2010
34
GB
Does anybody know of a way to disable users from using the Access 2007 application Close button?

The problem I’m having is that users occasionally fill out a form with data and then hit the Access 2007 application close button and the record still saves in the table. The intention of the user is to delete the record on exit of the database. I do have a custom close button that achieves this on the form using a macro with the undo run command. However, users are sometimes not using this custom close button and quitting the application from the Access 2007 close button instead.

Any help would be appreciated.
 
I've found the solution. Create a module and enter this code. You can then call the function DisableAccessCloseButton on the startup of your application.

Private Const GWL_STYLE = (-16)
Private Const WS_CAPTION = &HC00000
Private Const WS_MINIMIZEBOX = &H20000
Private Const WS_MAXIMIZEBOX = &H10000
Private Const WS_SYSMENU = &H80000
Const MF_BYCOMMAND = &H0&
Const MF_DISABLED = &H2&
Const MF_ENABLED = &H0&
Const MF_GRAYED = &H1&
Const SC_CLOSE = &HF060&

Private Declare Function apiEnableMenuItem Lib "User32" Alias "EnableMenuItem" (ByVal hMenu As Long, ByVal wIDEnableMenuItem As Long, ByVal wEnable As Long) As Long
Private Declare Function apiGetSystemMenu Lib "User32" Alias "GetSystemMenu" (ByVal hWnd As Long, ByVal Flag As Long) As Long

Function DisableAccessCloseButton(bEnable As Boolean, Optional ByVal lhWndTarget As Long = 0) As Long
On Error GoTo Handle_Error

'DisableAccessCloseButton(True) turns on the X close button
'DisableAccessCloseButton(False) turns off the X close button

Dim lhWndMenu As Long, lReturnVal As Long, lAction As Long
lhWndMenu = apiGetSystemMenu(iif(lhWndTarget = 0, Application.hWndAccessApp, lhWndTarget), False)
If lhWndMenu <> 0 Then
If bEnable Then
lAction = MF_BYCOMMAND Or MF_ENABLED
Else
lAction = MF_BYCOMMAND Or MF_DISABLED Or MF_GRAYED
End If
lReturnVal = apiEnableMenuItem(lhWndMenu, SC_CLOSE, lAction)
End If
DisableAccessCloseButton = lReturnVal

Exit_Process:
Exit Function

Handle_Error:
msgbox("Error: " & err.number & vbcrlf & errdescription
Resume Exit_Process

End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top