Closebutton vs. DoCmd.Close

G

Guest

Hi all...

I would like to exit out of a new record without executing any validation
code (in Before_Update) and without attempting to save the record. With a
command button this is easily done with a Me.Undo prior to closing, but how
do I do this if the user clicks on the CloseButton?

Any advice greatly appreciated,

Thanks, Jon.
 
G

Guest

Not sure, but you don't want the validation to work when there is a new record?

On the BeforeUpdate event of the form, in the start of sub write the code

If Me.NewRecord Then
Me.Undo
Exit Sub
End IF

Mybe there is something that I'm missing, not sure if there is a certain
criteria when you don't want to save the record
 
G

Guest

In addition to Ofers comment add your code to the On_Close of the form.
That's where you place the me.undo action.

But as Ofer stated it is not really clear what you are trying to accomplish.
You have to do some checking on the On_Close that meets your criteria...
 
M

missinglinq via AccessMonster.com

Many consider it a "fault" of Access that if you have a new record with
required fields empty, and hit the native "Close" button, Access closes
WITHOUT warning you or saving the new record!

--
There's ALWAYS more than one way to skin a cat!

Answers/posts based on Access 2000

Message posted via AccessMonster.com
 
R

RoyVidar

missinglinq via AccessMonster.com said:
Many consider it a "fault" of Access that if you have a new record
with required fields empty, and hit the native "Close" button, Access
closes WITHOUT warning you or saving the new record!

Or was it the other way around, that if you use the native close
button, the "x" in the upper right corner, you do get a warning,
while if you use DoCmd.Close you don't
http://allenbrowne.com/bug-01.html
 
8

88Caddy

Hmmm, I think I need to be more clear.

The user may add a record by entering text, options, etc and then
clicking on "Add". Alternatively the user may decide to exit the
record without completing it by clicking an "Exit" button or clicking
on the Closebutton; in this situation I would like to exit without
performing validation and without saving the record. I can capture
the "Exit" button click and perform a me.undo and a docmd.close.
However I cannot capture the Closebutton click, resulting in error/
validation messages that I do not want.
 
R

Rick Brandt

88Caddy said:
Hmmm, I think I need to be more clear.

The user may add a record by entering text, options, etc and then
clicking on "Add". Alternatively the user may decide to exit the
record without completing it by clicking an "Exit" button or clicking
on the Closebutton; in this situation I would like to exit without
performing validation and without saving the record. I can capture
the "Exit" button click and perform a me.undo and a docmd.close.
However I cannot capture the Closebutton click, resulting in error/
validation messages that I do not want.

Get rid of the close button and force them to use yours.
 
8

88Caddy

I did think of that... but I consider that a cheat, avoiding the
issue, and so on..... does anyone know of a way to do this
programmatically...??
 
D

Douglas J. Steele

Put logic in the form's Unload event. Call your Exit button's routine for
consistency's sake.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


88Caddy said:
I did think of that... but I consider that a cheat, avoiding the
issue, and so on..... does anyone know of a way to do this
programmatically...??
 
R

Rick Brandt

88Caddy said:
I did think of that... but I consider that a cheat, avoiding the
issue, and so on..... does anyone know of a way to do this
programmatically...??

You want to cancel an update. When you close a dirty form the events fire
in this order...

BeforeUpdate (this is where you can cancel an update)
AfterUpdate
Unload
Close

As you can see, by the time the Unload and Close events fire your record has
already been saved. You cannot do anything about it at that point. You can
of course use BeforeUpdate in both circumstances.

Your [Add] button could set a variable and then you can check that variable
in the BeforeUpdate event. That way you know when they pressed [Add] and
you can allow the save to proceed. When the variable indicates that they
have not pressed [Add] you can perform the Undo and Cancel the update.
 
D

Douglas J. Steele

Rick Brandt said:
You want to cancel an update. When you close a dirty form the events fire
in this order...

BeforeUpdate (this is where you can cancel an update)
AfterUpdate
Unload
Close

As you can see, by the time the Unload and Close events fire your record
has already been saved. You cannot do anything about it at that point.
You can of course use BeforeUpdate in both circumstances.

Oops. You're right, of course. Ignore my suggestion...
 
8

88Caddy

Excellent - I like this idea... thanks, I appreciate your help.

Jon.



88Caddy said:
I did think of that... but I consider that a cheat, avoiding the
issue, and so on..... does anyone know of a way to do this
programmatically...??

You want to cancel an update. When youclosea dirty form the events fire
in this order...

BeforeUpdate (this is where you can cancel an update)
AfterUpdate
UnloadClose

As you can see, by the time the Unload andCloseevents fire your record has
already been saved. You cannot do anything about it at that point. You can
of course use BeforeUpdate in both circumstances.

Your [Add] button could set a variable and then you can check that variable
in the BeforeUpdate event. That way you know when they pressed [Add] and
you can allow the save to proceed. When the variable indicates that they
have not pressed [Add] you can perform the Undo and Cancel the update.
 

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

Top