How can you force a bound form not to save a record.

Guus2005

AWF VIP
Local time
Today, 15:08
Joined
Jun 26, 2007
Messages
2,642
I don't like bound forms. Because of what happens when you're not watching.

I've inherited an application in which there are several bound forms.:(

The customer now wants a save button on one of the forms.
It is easy to make that work
Code:
DoCmd.RunCommand acCmdSaveRecord

But what if the record is cancelled?

The autonumber ID field gives me a number which record i can delete from the table easily. But the damage is already done.

Is there another way?

If tried this also.
Code:
me.Undo
sendkeys "{ESC}"

Awaiting your instructions and witty comments.
 
Cancelled after the Save button is pressed?

If so, that's just tough, indecisive users can't be accounted for to that extent. You'd have to delete the record.

If not, then Me.Undo should do it. Put that in the Cancel buttons click event.

(Problems happen if there are subforms or other reasons the record will save early. Then you need code in the Form_BeforeUpdate event asking the user to confirm the changes.)
 
Cancelled after the Save button is pressed?

If so, that's just tough, indecisive users can't be accounted for to that extent. You'd have to delete the record.

If not, then Me.Undo should do it. Put that in the Cancel buttons click event.

(Problems happen if there are subforms or other reasons the record will save early. Then you need code in the Form_BeforeUpdate event asking the user to confirm the changes.)
Even before the save button is pressed, the new record is already made.
It is created the moment you enter data in one of the fields.
That is the moment the Autonumber field create a new number.
Even the BeforeUpdate event is too late.
You can't program the BeforeInsert event because that prevents the user from entering data and AfterInsert is too late.

Bound forms are a pain.
 
So what is the specific issue? "Sequenced" autonumbers? The autonumber may be gone on Undo but the record is not saved before it is saved, so to speak.
 
Even before the save button is pressed, the new record is already made.
It is created the moment you enter data in one of the fields.

Made? It isn't saved then. I think that's why you hate bound forms - a misunderstanding. No record can ever save without going through the form's before update event.

A new record is not saved until after the form is updated and you can capture that and cancel it.
 
Try pressing the Escape Key once a new record is created.

Does it disappear?

Whitty Responce...................

A bad tradesman always blames his tools.

End Whitty Responce..............................
 
I hate musical instruments! They're useless. I once tried to play one and it sounded horrible.
 
Even before the save button is pressed, the new record is already made.
It is created the moment you enter data in one of the fields.
That is the moment the Autonumber field create a new number.
Even the BeforeUpdate event is too late.
You can't program the BeforeInsert event because that prevents the user from entering data and AfterInsert is too late.

Bound forms are a pain.
The autonumber is assigned as soon as the user types the first character in any field - if the form is bound to a Jet/ACE table. If your BE is SQL Server or Oracle or DB2, etc., the autonumber (identity column) isn't assigned until the row is added at the server. Autonumbers are used to provide a unique identifier for a record. They should never be assigned any meaning. Gaps are possible, probable even because if a record is started but the save is cancelled, the autonumber is discarded. This is necessary because Access is multi-user out of the box and another user could have added a row to the same table while the first user was filling in the form. So, Access simply discards the number rather than attempting to reuse it. If you want to create your own sequence number, you have better control. The best technique would be to assign the sequence numbe as the last statement in the BeforeUpdate event so it is assigned at the last possible moment. You still could run into a conflict if another user was saving at about the same time and so generated the same number. Your code will need to take this into consideration and increment the seq number and loop until an insert is successful.

As vila already mentioned - no record gets saved without the Form's BeforeUpdate event being executed so that is where you do your final edits and cancel the update if the validation fails.
 
On a bound form with an Access BE, records are implicitly saved when closing the form or when moving to a previous record.

My autonumber fields are used as PK's no need for them to be sequential.

Between the witty comments, (Vilarestal) en crappy phrases (thank you Rainlover, congratz you have made my shitlist!) there was a gem. At least i hope it is.

Cancelling the BeforeUpdate event should prevent the record from being saved.

Thanks. I will try that one.

Thanks everyone.
 

Users who are viewing this thread

Back
Top Bottom