Stop a form to close

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

Hi

How can I stop the action close form

When the user close the form I'm asking him if he want to
close or no and when he said no (I don't want to close
the form) the action close form will stop and the form
will stay loaded at screen

Can I do that

This is the code I'm using:

Private Sub Form_Close()
If (Me.Item) = 0 And IsNull(Me.CoulorA) And IsNull
(Me.CoulorB) And IsNull(Me.CouleurC) Then
If MsgBox("Are you sure to quit the form without
adding a command", vbExclamation + vbYesNo) = vbYes Then
DoCmd.SetWarnings True
DoCmd.RunSQL "DELETE * FROM Tb_commande WHERE
CommandeID = " & Me.CommandeID
DoCmd.SetWarnings False
DoCmd.DoMenuItem acFormBar, acRecordsMenu,
acSaveRecord, , acMenuVer70
Forms!FrmCommande!SbFrm_Commande.Requery

Else
---------------WHAT CAN I ADD HERE TO STOP THE
UNLOAD OF THE FORM
End If
End If
End Sub
 
Chris,

Below from Access Help:

<SNIP>
The Close event occurs after the Unload event, which is triggered after the
form is closed but before it is removed from the screen.
When you close a form, the following events occur in this order:

Unload ? Deactivate ? Close

When the Close event occurs, you can open another window or request the
user's name to make a log entry indicating who used the form or report.

The Unload event can be canceled, but the Close event can't.

<SNIP>

HTH,

Josh
 
1. By the time the Form_Close Event occurs, it is to late to stop / cancel
the closing of the Form. Normally you can use the Form_Unload Event for
straight out cancellation but in your case, you want to check the data
entries as well so you need to use an earlier event, possibly the
Form_BeforeUpdate Event.

2. If you want to *prevent* the Confirmation (of the Delete Query) from
popping up, I think you got the SetWarnings statements wrong way around.

3. Not sure what you want to do in your code but it seems the action
supposedly to be done by the statement:

DoCmd.DoMenuItem acFormBar, acRecordsMenu, _
acSaveRecord, , acMenuVer70

contradicts with the logic of the question in the MsgBox and the flow of
your code: if the user answer Yes (quit without saving), the above statement
tries to save the Record???

At present, this statement doesn't do any thing since the Record has been
saved (just after the Form_BeforeUpdate Event, much earlier than the
Form_Close Event) so you probably did not see the contradiction.

4. BTW, DoMenuItem is really old code (you used the Access95 Menu in the
above statement). Suggest you use later code for the above like:

DoCmd.RunCommand acCmdSaveRecord
 
Van,

True, DoMenuItem is old, but why does Access Button Wizard still use it?

Josh
 
Since A97, Access Help recommends not to use DoMenuItem but unfortunately,
Microsoft haven't got around to update the CommandButton Wizard ...
 
A cheap work around is to disable the close button (ie.
red square) and create your own close button.

My question though is can I stop the MS Access Database
from closing? I figure once/if I compile the database it
wont be an issue but still in the mean time it would be
nice to know how.
 
Thanks, Josh

Your ideas help me
.....
-----Original Message-----
Chris,

Below from Access Help:

<SNIP>
The Close event occurs after the Unload event, which is triggered after the
form is closed but before it is removed from the screen.
When you close a form, the following events occur in this order:

Unload ? Deactivate ? Close

When the Close event occurs, you can open another window or request the
user's name to make a log entry indicating who used the form or report.

The Unload event can be canceled, but the Close event can't.

<SNIP>

HTH,

Josh




.
 
Back
Top