Required Property

  • Thread starter Thread starter Mark
  • Start date Start date
M

Mark

Access97 ---

I set the Required property for a field at the table level and I have a form
that contains that field. When I click the Close button at the top left of the
screen, I get an error message when the field is blank and Access will not let
me save the record. However, I also have a Close command button with the code:
DoCmd.Close
When I click the button, the form closes with no error message although the
field is blank. I tried changing the code to:
DoCmd.Close acForm, "MyForm"
and made no difference; the form closed although the field was blank.

What is the difference between closing the form with the form's close button and
closing the form with a coded command button? Can the Required property be made
to work with a coded command button?

Thanks!

Mark
 
Hi Mark.

The Required property at the engine level does work, i.e. the record is not
saved with the field blank. The problem is with the Close action.

When you use the Close action/method, Access silently *loses* your entry
without any message to notify you. To me, that is a major data-loss bug in a
piece of software that is supposed to save entries by default, but all my
attempts to get MS to fix this (over many years) have failed.

If it is any comfort, you are just one in a long queue of people who have
been stung by this. Many just lose data without ever figuring out why
"Access sometimes doesn't save properly."

Once you know about this bug, it is not too difficult to work around it:
always force an explicit save before you issue a Close. Example:
If Me.Dirty Then
Me.Dirty = False
End If
DoCmd.Close acForm, Me.Name
That code raises an error if Dirty is not set to False (i.e. the record
cannot be saved), so control passes to your error handler and the Close is
never called.

For some of the other unfixed flaws that remain unfixed through many
versions of Access, see:
http://members.iinet.net.au/~allenbrowne/tips.html#flaws
 
Mark said:
Access97 ---

What is the difference between closing the form with the form's close button and
closing the form with a coded command button? Can the Required property be made
to work with a coded command button?


The sort answer, I believe, is "no." I'd recommend a short bit of
code that manually checks the current status of the required field(s)
in the On Unload (NOT On Close) event for the form. You can execute a
CancelEvent action to stop the form from closing if the field is
empty. (There's a great bit of example code in the CancelEvent help
for Access 97.)

Depending on how you want to treat the situation, you could go the
extra mile and include a message box, set the focus to the required
field's data entry point, and temporarily re-color that field's
background color to red.

Dennis
 
Back
Top