Prevent someone from closing Access.

  • Thread starter Thread starter J. Shrimp, Jr.
  • Start date Start date
J

J. Shrimp, Jr.

Need to have users close access only from the form
open at that time (form has docmd.quit on close event)
Is it possible to mask the standard Access Window
so only the forms show?
 
I use this.

Put this in a module:

*****************************
'traps the event if the user tries to close the database by clicking the
"x",
'by using the Task Manager, or by hitting Alt+F4
'can only exit by clicking the exit button on frmMain

Public Function AllowExit() As Boolean
On Error GoTo Error
If Forms!frmMain!cmdExit.Tag = "allow exit" Then
AllowExit = True
Else
AllowExit = False
msg1 = "To close this application, first close all forms and "
msg2 = "then click the 'Exit' button on the Main form."
style = vbExclamation
answer = MsgBox(msg1 & vbCrLf & msg2, style, conTitle)
End If

ExitFunction:
Exit Function

Error:
MsgBox Err.Number & vbCrLf & Err.Description & vbCrLf & "Function
AllowExit"
Resume ExitFunction
End Function
*****************************

This in your form with the Exit button:

*****************************
Private Sub Form_Open(Cancel As Integer)
On Error GoTo GotError
'can only exit on frmMain
Me!cmdExit.Tag = vbNullString
....
End Sub
*****************************
Private Sub Form_Unload(Cancel As Integer)
On Error GoTo GotError
'can only exit on frmMain
If AllowExit = False Then
Cancel = True
End If

ExitSub:
Exit Sub

GotError:
MsgBox Err.Number & vbCrLf & Err.Description & vbCrLf & "Main
Form_Unload"
Resume ExitSub
End Sub
***********************
Private Sub cmdExit_Click()
On Error GoTo GotError
'can only exit on frmMain
Me!cmdExit.Tag = "allow exit"
Application.Quit

ExitSub:
Exit Sub

GotError:
MsgBox Err.Number & vbCrLf & Err.Description & vbCrLf & "Main
cmdExit_Click"
Resume ExitSub
End Sub
***********************

HTH,
Debbie


| Need to have users close access only from the form
| open at that time (form has docmd.quit on close event)
| Is it possible to mask the standard Access Window
| so only the forms show?
 
What I do is to hide the menubar and disable the x on the
form close and Application Quit. The only way to leave a
form or application is with command buttons on forms.

Hide the menubar
On each forms properties
On Menu bar property
=1

To disable the Close(X) on the form
Close button property
No

To disable the QuitApplication(X) - From Microsoft

Start Microsoft Access.
Open the sample database Northwind.mdb.
On the Insert menu, click Class Module.
Type the following code into the Declarations section:
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

Const MF_GRAYED = &H1&
Const MF_BYCOMMAND = &H0&
Const SC_CLOSE = &HF060&

Add the following procedures to the class module:
Public Property Get Enabled() As Boolean
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
End Property

Public Property Let Enabled(boolClose As Boolean)
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)
End Property

On the File menu, click Save Northwind (to save the
project), and when prompted for the name of the class
module, save it as CloseCommand.
On the Insert menu, click Module to create a new, standard
module.
Add the following procedure to the module:
Function InitApplication()
Dim c As CloseCommand
Set c = New CloseCommand

'Disable Close menu.
c.Enabled = False
End Function

On the Debug menu, click Compile Northwind. If the project
is not compiled successfully, correct the compilation
errors, and then compile the project again.
On the File menu, click Save Northwind, and use the default
name that appears in the Module Name box by clicking OK.
Create a new macro with the following actions and action
arguments:
Action
-------
RunCode

Action Arguments
-------------------------------
Function Name: InitApplication()

Save the macro and name it Autoexec.
Close the database.
Reopen the database.
Note that the Close button and the Close command on the
System menu of the application window are disabled.
Usage
The CloseCommand class module described in this article
allows you to easily enable or disable the Close button and
the Close command of the application window. The class
module also allows you to check the state of these commands
to determine if they are currently enabled or disabled.
Before doing either of these, your code must first create
an instance of the CloseCommand class, as demonstrated in
the InitApplication function earlier in this article.

To check the state of the Close button, refer to the
Enabled property of the CloseCommand instance that your
code created. Likewise, to set the state of the Close
button, assign True or False to the Enabled property of the
CloseCommand instance that your code created.

Please note that this technique affects the Close button on
the application window of Microsoft Access, not the Close
button on the Database window. After disabling the Close
button, the button is not automatically reenabled when your
database closes. If the user closes the database and leaves
Microsoft Access open, the user will not be able to quit
Microsoft Access by using the Close button. In this case,
your application should reenable the Close button before it
terminates. Otherwise, the user will have to quit and
restart Microsoft Access in order for the Close button to
be enabled.

This technique does not disable the Exit command on the
File menu. If your application needs to disable this
command, you must customize the File menu to remove the
Exit command.
Last Reviewed: 5/13/2002
Keywords: KB245746

Good Luck

Chris
 
Back
Top