Well this one has got me totally baffled, so I'm hoping someone has an easy answer.
Here's the general scenario: A simple form frmForm1 is bound to a table tblTable1. The user makes the current record dirty by editing one of the bound controls on the form. He she then clicks on the form's Close Window button or the Access Close button.
Here's what I WANT to happen: Because the record is dirty, I want to prevent closure of the form (and of course the application). Now I know that I can make the form modal, and optionally hide the control buttons, and take control of the Alt F4 keys and so on, but WHY wont the following code to the whole job:
Dim AllowFormExit
Private Sub Form_Error(DataErr As Integer, Response As Integer)
Response = acDataErrContinue
End Sub
Private Sub Form_Unload(Cancel As Integer)
If Not AllowFormExit Then
Cancel = True
End If
End Sub
Private Sub btnClose_Click()
AllowFormExit = True
DoCmd.Close
End Sub
Notes:
(a) The AllowFormExit variable just allows me to close the form. Otherwise, the Form_UnLoad event prevents any closure of the form (as is my intent in this example).
(b) The Cancel in the BeforeUpdate event SHOULD prevent any record from being changed.
(c) The "Response = acDataErrContinue" statement in the form_error event prevents the following error from being displayed: "You can't save this record at this time. Microsoft Access may have encountered an error while trying to save a record. If you close this object now, the data changes you made will be lost. Do you want to change the database object anyway? (yes) (no).
(d) If I comment out the above statement, then the above message is displayed when I press the Close Window button on the form. Responding No, leaves the record dirty, which is my intended result.
(e) If I leave the "Response = acDataErrContinue" statement in the Form_Error event, then the message is suppressed, the window is left open, but the CURRENT RECORD IS SAVED (ie. seemingly taking the course of the (Yes) option associated with the above message.
Now here's the big question; Is their any way in which I can get Access to take the No option (ie. leave the record dirty) without the prompt I dont seem to be able to intercept this message in any way; even if I cancel the beforeupdate event.
I've probably made a hash of trying to explain this problem, but if anyone has any ideas on effectively getting Access to TOTALLY IGNORE any clicks on the Form or Windows Close buttons, I'd really like to hear them (or is suppression of these buttons the only solution?)
Many thanks,
Steve
Here's the general scenario: A simple form frmForm1 is bound to a table tblTable1. The user makes the current record dirty by editing one of the bound controls on the form. He she then clicks on the form's Close Window button or the Access Close button.
Here's what I WANT to happen: Because the record is dirty, I want to prevent closure of the form (and of course the application). Now I know that I can make the form modal, and optionally hide the control buttons, and take control of the Alt F4 keys and so on, but WHY wont the following code to the whole job:
Dim AllowFormExit
Private Sub Form_Error(DataErr As Integer, Response As Integer)
Response = acDataErrContinue
End Sub
Private Sub Form_Unload(Cancel As Integer)
If Not AllowFormExit Then
Cancel = True
End If
End Sub
Private Sub btnClose_Click()
AllowFormExit = True
DoCmd.Close
End Sub
Notes:
(a) The AllowFormExit variable just allows me to close the form. Otherwise, the Form_UnLoad event prevents any closure of the form (as is my intent in this example).
(b) The Cancel in the BeforeUpdate event SHOULD prevent any record from being changed.
(c) The "Response = acDataErrContinue" statement in the form_error event prevents the following error from being displayed: "You can't save this record at this time. Microsoft Access may have encountered an error while trying to save a record. If you close this object now, the data changes you made will be lost. Do you want to change the database object anyway? (yes) (no).
(d) If I comment out the above statement, then the above message is displayed when I press the Close Window button on the form. Responding No, leaves the record dirty, which is my intended result.
(e) If I leave the "Response = acDataErrContinue" statement in the Form_Error event, then the message is suppressed, the window is left open, but the CURRENT RECORD IS SAVED (ie. seemingly taking the course of the (Yes) option associated with the above message.
Now here's the big question; Is their any way in which I can get Access to take the No option (ie. leave the record dirty) without the prompt I dont seem to be able to intercept this message in any way; even if I cancel the beforeupdate event.
I've probably made a hash of trying to explain this problem, but if anyone has any ideas on effectively getting Access to TOTALLY IGNORE any clicks on the Form or Windows Close buttons, I'd really like to hear them (or is suppression of these buttons the only solution?)
Many thanks,
Steve