> Dear all,
>
[quoted text clipped - 7 lines]
>
> Vensia
If you do your validation in BeforeUpdate and set the Cancel argument of that
event to True when the validation fails then that will prevent the form from
closing if closing the form was what triggered the BeforeUpdate.
In other words if you write your BeforeUpdate code properly then the Close and
Unload events will never even occur.
The exception to this is if you have created your own close button. In that
case Access has a bug wherein the invalid record is silently discarded and the
form still closes. That can be avoided by having your custom close code
explicitly save the record prior to closing the form. Then the above will still
apply and the form will not close unless BeforeUpdate validation passes.

Signature
I don't check the Email account attached
to this message. Send instead to...
RBrandt at Hunter dot com
Vensia - 12 Dec 2005 04:31 GMT
My problem is I have my own close button.
I don't know how to write the code at the Unload event to force the
BeforeUpdate validation be passed.
I want the user can close the form if he/she cancel the record
editing/insertion by pressing Esc button.
Thanks.
> > Dear all,
> >
[quoted text clipped - 25 lines]
> to this message. Send instead to...
> RBrandt at Hunter dot com
Rick Brandt - 12 Dec 2005 12:31 GMT
> My problem is I have my own close button.
> I don't know how to write the code at the Unload event to force the
> BeforeUpdate validation be passed.
> I want the user can close the form if he/she cancel the record
> editing/insertion by pressing Esc button.
> Thanks.
Don't put it in the Unload event. Put it into the OnClick code of your close
button.
Me.Dirty = False
DoCmd.Close
The first line will trigger the BeforeUpdate if the form is dirty. If that is
cancelled then that will be treated as an error by your on-click code. Just
test for that error in your error handler and ignore it. Only if BeforeUpdate
is not cancelled will the line to close the form execute.

Signature
I don't check the Email account attached
to this message. Send instead to...
RBrandt at Hunter dot com
Vensia - 12 Dec 2005 15:32 GMT
But the user can still close the form by using CTRL + F4 or pressing X sign
(right corner above)
> > My problem is I have my own close button.
> > I don't know how to write the code at the Unload event to force the
[quoted text clipped - 18 lines]
> to this message. Send instead to...
> RBrandt at Hunter dot com
Rick Brandt - 12 Dec 2005 17:29 GMT
> But the user can still close the form by using CTRL + F4 or pressing
> X sign (right corner above)
And those will also trigger the BeforeUpdate event (if the record is dirty)
and if you cancel that event the form will not close.

Signature
I don't check the Email account attached
to this message. Send instead to...
RBrandt at Hunter dot com
Vensia - 13 Dec 2005 03:07 GMT
Dear Rick,
I have wrote the code like this :
Private Sub cmdClose_Click()
On Error GoTo Err_cmdClose_Click
Me.Dirty = False
DoCmd.Close
Exit_cmdClose_Click:
Exit Sub
Err_cmdClose_Click:
If Err.Number = 2101 Then
Else
MsgBox Err.Number
MsgBox Err.Description, vbInformation, "Close"
End If
Resume Exit_cmdClose_Click
End Sub
But when I click the X (close button at the right above corner), I get the
validation message and then this following message :
You can't save this record at this time.
.........
........
If you close this object now, the data changes you made will be lost.
Do you want to close the database object anyway ?
Then I click the Yes button, after that, the form can be closed.
Thanks.
> > But the user can still close the form by using CTRL + F4 or pressing
> > X sign (right corner above)
[quoted text clipped - 6 lines]
> to this message. Send instead to...
> RBrandt at Hunter dot com
Vensia - 12 Dec 2005 04:37 GMT
In the Unload event, I can't use Me.Dirty to check the record is being
updated or not.
> > Dear all,
> >
[quoted text clipped - 25 lines]
> to this message. Send instead to...
> RBrandt at Hunter dot com