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,
>
[quoted text clipped - 19 lines]
>
> DubboPete
DubboPete - 23 Mar 2005 10:19 GMT
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,
>
[quoted text clipped - 44 lines]
>>
>> DubboPete
Nikos Yannacopoulos - 23 Mar 2005 13:23 GMT
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,
>
[quoted text clipped - 80 lines]
>>>
>>>DubboPete