Closing an Access document

T

Tulio

how can I prevetn a user from closing an Access document by clicking on the uper right hand side X button?
 
J

John Vinson

how can I prevetn a user from closing an Access document by clicking on the uper right hand side X button?

An Access database isn't really a "document" - it's a container for
multiple forms, tables, reports and other objects.

To block closing a Database prematurely, you must open a Form (you can
then make it invisible if you wish) when the database opens. Put VBA
code in that Form's Close event to check whether a condition has been
set to allow closing, and Cancel the close event if not.

For example, you could define a Global Variable:

Public bOKToClose As Boolean

In the code for whatever event you want to close the database (a
button click for instance) set this variable to True. In the "trap"
form you'ld have code like

Private Sub Form_Close(Cancel as Integer)
If Not bOKToClose Then
MsgBox "Please close the application using the button"
Cancel = True
End If
End Sub

Be sure not to make it *too* difficult for the user to close -
otherwise they may get frustrated and ctrl-alt-delete and kill the
program, which can corrupt your database!
 
T

Tulio

Thanks for your help but I am not try to avoid closing a form but the entire database itself. That is what I trying to avoid. In the MVP website there some code for that but for some reason the logic is failing. It works well for minimazing the DB but not to hide which is probably what I am lookig for. I get an error message that reads "Cannot hide Access unless a form is on screen". See code below

Global Const SW_HIDE =
Global Const SW_SHOWNORMAL =
Global Const SW_SHOWMINIMIZED =
Global Const SW_SHOWMAXIMIZED =

Private Declare Function apiShowWindow Lib "user32"
Alias "ShowWindow" (ByVal hwnd As Long,
ByVal nCmdShow As Long) As Lon

Function fSetAccessWindow(nCmdShow As Long

Dim loX As Lon
Dim loForm As For
On Error Resume Nex
Set loForm = Screen.ActiveFor
If Err <> 0 Then 'no Activefor
If nCmdShow = SW_HIDE The
MsgBox "Cannot hide Access unless "
& "a form is on screen
Els
loX = apiShowWindow(hWndAccessApp, nCmdShow
Err.Clea
End I
Els
If nCmdShow = SW_SHOWMINIMIZED And loForm.Modal = True The
MsgBox "Cannot minimize Access with "
& (loForm.Caption + " ")
& "form on screen
ElseIf nCmdShow = SW_HIDE And loForm.PopUp <> True The
MsgBox "Cannot hide Access with "
& (loForm.Caption + " ")
& "form on screen
Els
loX = apiShowWindow(hWndAccessApp, nCmdShow
End I
End I
fSetAccessWindow = (loX <> 0
End Functio
 
J

John Vinson

Thanks for your help but I am not try to avoid closing a form but the entire database itself. That is what I trying to avoid. In the MVP website there some code for that but for some reason the logic is failing. It works well for minimazing the DB but not to hide which is probably what I am lookig for. I get an error message that reads "Cannot hide Access unless a form is on screen". See code below:

I'm not sure I'm understanding.

If you are trying to prevent users from closing a database, the
recommended method is to open a Form in that database, and use that
Form's Close event to prevent closing the form until you want the
database closed. If the Form is kept open, so is the database.

I really have no idea what minimizing or hiding either the form or the
database have to do with this!
 

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