Code to cause a form update...


John Keith

Access 2003:

I have an add record form with a key field and several other fields.
Since the key is the only required field, I have a cmd button that runs this
statement: "DoCmd.Close acForm, Me.Name, acSaveYes" which saves the record
and closes the form then requeries a main form to show this new record in the
selection list.

When tabing off the form (instead of clicking the add record button) the
AfterUpdate event fires. I am thinking that my close form and requery code
should be in the AfterUpdate so there is one place to maintain this code.
How do I make the btn click routine cause the form to be updated and thus
fire the AfterUpdate event? (duplicating the same code in both places
(Addbutton_click and AfterUpdate)) throws an error that says the form close
event was canceled.

I have a cancel button that runs Me.Undo which will cancel the form changes;
I need the Me.DoTheSave command code...
Perhaps there a DoCmd that will accomplish this? Seems messy to just code
around the if form is not open in the AfterUpdate event.

John Keith

I guess I should have searched help a bit more...

In the btnAdd_Click routine:
DoCmd.Save acForm, Me.Name

Dirk Goldgar

John Keith said:
I guess I should have searched help a bit more...

In the btnAdd_Click routine:
DoCmd.Save acForm, Me.Name

No. That does not save the form's modified record, which I think is what
you're trying to do. That saves design changes that have been made to the
form, which is not usually what you want.

There are several ways to tell Access to save the current form's record.
One is:

RunCommand acCmdSaveRecord

Another, which I like better because it makes it very clear exactly *which*
form's record is to be saved, is this:

If Me.Dirty Then Me.Dirty = False

That line only attempts to save the record if the record has actually been

Please note: this code from your other post:

.... also saves design changes to the form. If that is not what you want,
then specify acSaveNo:

DoCmd.Close acForm, Me.Name, acSaveNo

The *record* will still be saved (if it has been modified), because Access
always saves a modified record -- if other constraints don't prevent it from
being saved -- when you close the form or move to a new record.

John Keith

Thanks for the clarification... Im going to use the explicit check on dirty
as a standard from now on.

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