Stopping erroneous closure of app/forms

D

DubboPete

Hi all,

I am trying to trap [and stop] instances where users close the app by
clicking the MS Access main Window [X] button in the top right of the
screen. You know the one, it closes everything.... I want them to close
the app via the menu option 'quit database'.

so...

Firstly, is there a way of capturing that action and then performing a check
on whether the user really wants to quit?

Secondly, if the user closes the active form (the only form open), can one
prompt the user to halt their actions (as there will not be any visible
forms if they do so) ?

tia
and hope the Easter Bunny visits those good boys and girls that deserve a
visit....

from Dubbo's 'only' 17th-century Goat Herder

DubboPete
 
N

Nikos Yannacopoulos

Pete,

The "standard" trick to enforce a piece of code to run before a database
is closed, no matter how the user closes it, is to put it in the On
Close event of a form that is open at all times - a switchboard, if one
exists, or a dummy, hidden form that serves the very purpose alone.

Now, the simplest way to ensure the user doesn't close a particular form
is to disable the close button, or the whole control box, in the form's
properties... or is it that you need to make sure that at least one form
will remain open, but that can be any of anumber of forms, as opposed to
a particular one? In the latter case, you need to do the following for
all forms:
1. disable the close button or control box
2. add a command button for closing, with the following code:

If Forms.Count = 1 Then
MsgBox "You can't close the last form!"
Exit Sub
End If
DoCmd.Close acForm, Me.Name

HTH,
Nikos
 
D

DubboPete

Thanks Nikos,

The database has one form visible, for example, FrmFocus
with the odd occasion when

Forms!FrmMainMenu.Visible = False

There is only ever one visible form, and mostly one invisible form, but I
can
set up the dB so that at least one form is visible=false, as in your
"or a dummy, hidden form that serves the very purpose alone"

Actually, I need a form that serves many other purposes, but will not be
visible, so that answers that one... one form will always be visible=false

I try and kill all control boxes where possible on forms, so close/minmax
buttons
are not available. (Hence the reason for the code in the first place...)

Will this code (not yet tried it, cos I am at home, and the dB is
at work) do that job?

and the MsgBox will say something like (and then perform):

If Forms.Count = 1 Then
MsgBox "This is not the way to close the database. Please use the menu
option to maintain data integrity"
Exit Sub
End If
DoCmd.Close acForm, Me.Name
Docmd.OpenForm "FrmMistake"

thanks for your help mate

The Goat Herder (DubboPete), circa 1605

Nikos Yannacopoulos said:
Pete,

The "standard" trick to enforce a piece of code to run before a database
is closed, no matter how the user closes it, is to put it in the On Close
event of a form that is open at all times - a switchboard, if one exists,
or a dummy, hidden form that serves the very purpose alone.

Now, the simplest way to ensure the user doesn't close a particular form
is to disable the close button, or the whole control box, in the form's
properties... or is it that you need to make sure that at least one form
will remain open, but that can be any of anumber of forms, as opposed to a
particular one? In the latter case, you need to do the following for all
forms:
1. disable the close button or control box
2. add a command button for closing, with the following code:

If Forms.Count = 1 Then
MsgBox "You can't close the last form!"
Exit Sub
End If
DoCmd.Close acForm, Me.Name

HTH,
Nikos

Hi all,

I am trying to trap [and stop] instances where users close the app by
clicking the MS Access main Window [X] button in the top right of the
screen. You know the one, it closes everything.... I want them to
close the app via the menu option 'quit database'.

so...

Firstly, is there a way of capturing that action and then performing a
check on whether the user really wants to quit?

Secondly, if the user closes the active form (the only form open), can
one prompt the user to halt their actions (as there will not be any
visible forms if they do so) ?

tia
and hope the Easter Bunny visits those good boys and girls that deserve a
visit....

from Dubbo's 'only' 17th-century Goat Herder

DubboPete
 
N

Nikos Yannacopoulos

Pete,

If you are keeping a form invisible (and open) at all times, then just
use its On Close event to run whatever code your Exit menu item runs,
and that takes care of it, no need to tell the user to use the menu item.

Now, as far as keeping at least one form open (and visible) goes, I'm
afraid the code I provided previously will not work as is, if there is
an hidden form, because that one is counted too. If there is always one
and only one hidden form, then just change the codnition form 1 to 2
(form being closed plus hidden form). If the number of hidden forms may
vary, then you need some more code to check if the one being closed is
the last visible one:

Function visible_forms_count()
X = 0
For Each frm In Forms
If frm.Visible = True Then X = X + 1
Next
visible_forms_count = X
End Function

will count the visible forms, so the code behind the close command
button becomes:

If visible_forms_count = 1 Then
MsgBox "You can't close the last form!"
Exit Sub
End If
DoCmd.Close acForm, Me.Name

HTH,
Nikos
Thanks Nikos,

The database has one form visible, for example, FrmFocus
with the odd occasion when

Forms!FrmMainMenu.Visible = False

There is only ever one visible form, and mostly one invisible form, but I
can
set up the dB so that at least one form is visible=false, as in your
"or a dummy, hidden form that serves the very purpose alone"

Actually, I need a form that serves many other purposes, but will not be
visible, so that answers that one... one form will always be visible=false

I try and kill all control boxes where possible on forms, so close/minmax
buttons
are not available. (Hence the reason for the code in the first place...)

Will this code (not yet tried it, cos I am at home, and the dB is
at work) do that job?

and the MsgBox will say something like (and then perform):

If Forms.Count = 1 Then
MsgBox "This is not the way to close the database. Please use the menu
option to maintain data integrity"
Exit Sub
End If
DoCmd.Close acForm, Me.Name
Docmd.OpenForm "FrmMistake"

thanks for your help mate

The Goat Herder (DubboPete), circa 1605

Pete,

The "standard" trick to enforce a piece of code to run before a database
is closed, no matter how the user closes it, is to put it in the On Close
event of a form that is open at all times - a switchboard, if one exists,
or a dummy, hidden form that serves the very purpose alone.

Now, the simplest way to ensure the user doesn't close a particular form
is to disable the close button, or the whole control box, in the form's
properties... or is it that you need to make sure that at least one form
will remain open, but that can be any of anumber of forms, as opposed to a
particular one? In the latter case, you need to do the following for all
forms:
1. disable the close button or control box
2. add a command button for closing, with the following code:

If Forms.Count = 1 Then
MsgBox "You can't close the last form!"
Exit Sub
End If
DoCmd.Close acForm, Me.Name

HTH,
Nikos

Hi all,

I am trying to trap [and stop] instances where users close the app by
clicking the MS Access main Window [X] button in the top right of the
screen. You know the one, it closes everything.... I want them to
close the app via the menu option 'quit database'.

so...

Firstly, is there a way of capturing that action and then performing a
check on whether the user really wants to quit?

Secondly, if the user closes the active form (the only form open), can
one prompt the user to halt their actions (as there will not be any
visible forms if they do so) ?

tia
and hope the Easter Bunny visits those good boys and girls that deserve a
visit....

from Dubbo's 'only' 17th-century Goat Herder

DubboPete
 

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