The form can not be closed if the data entry is incomplete

  • Thread starter Thread starter Vensia
  • Start date Start date
V

Vensia

Dear all,

I have a bound customer form and the user should input customer code and
customer name.
I have validation code at the Form_BeforeUpdate. I want the form can't be
closed if the validation rule is violated.
How to write the code in Form_Unload event to prevent user quit from the
form ?
Thanks.

Vensia
 
Vensia said:
Dear all,

I have a bound customer form and the user should input customer code
and customer name.
I have validation code at the Form_BeforeUpdate. I want the form
can't be closed if the validation rule is violated.
How to write the code in Form_Unload event to prevent user quit from
the form ?
Thanks.

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.
 
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.
 
Vensia said:
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.
 
But the user can still close the form by using CTRL + F4 or pressing X sign
(right corner above)
 
Vensia said:
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.
 
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.
 

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

Back
Top