Trigger 'x' button behavior from code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Ok, this is probably a dumb question, but, I want to emulate in code using a
button the same exact behavior as the 'X' on the form.

I do not want to use
DoCmd.Close acForm, Me.Name
as its not playing nice with my error checking.

I'm sure I'm overlooking the obvious, but then again it is Friday. :S
 
What you've posted is the exact code to use for closing the form in which
the code is running. What isn't working with error checking? Tell us more
details.
 
DBG said:
Ok, this is probably a dumb question, but, I want to emulate in code
using a button the same exact behavior as the 'X' on the form.

I do not want to use
DoCmd.Close acForm, Me.Name
as its not playing nice with my error checking.

I'm sure I'm overlooking the obvious, but then again it is Friday. :S

The main difference is that DoCmd.Close doesn't display a message if
there's a data error or validation that prevents the current record from
being saved. Instead, the record is silently discarded. This a bug, in
many people's opinion.

What you can do is force a record save before calling DoCmd.Close; for
example,

If Me.Dirty Then Me.Dirty = False

DoCmd.Close acForm, Me.Name

If the record can't be saved, an error will be raised on executing
"Me.Dirty = False", and you can handle that error any way you like.
 
The main difference is that DoCmd.Close doesn't display a message if
there's a data error or validation that prevents the current record from
being saved. Instead, the record is silently discarded. This a bug, in
many people's opinion.

Yes, it is the record being silently discarded which has been throwing me
for a loop.
Using the X allows me to discard the record and exit if the user chooses to
ignore my custom form validation handling.
What you can do is force a record save before calling DoCmd.Close; for
example,

If Me.Dirty Then Me.Dirty = False

DoCmd.Close acForm, Me.Name

If the record can't be saved, an error will be raised on executing
"Me.Dirty = False", and you can handle that error any way you like.

Throws 2101 which I can trap and work with, thanks.
 
Back
Top