Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Access VBA Code to stop data saving to tables

Hi

I have an Access form that was generated using a wizard.
I want to put an "OK" and "Cancel" button on the form so that data
is not commit to the underlying table unless the OK button is used.

So far I have the following code that runs on the click of the "Cancel" button:
  If Me.NewRecord Then
        If Me.Dirty Then
            Me.Undo
        End If
    End If
    DoCmd.Close

Open in new window

Where else should I put this or similar code to stop data being saved unless the user clicks the "OK" button?
One major concern is that the user clicks an arrow button to move to the next record. How do I stop that?
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Use the BeforeSave event of the record.
Set parameter Cancel to True if you don't want the record to be saved.
He meant BeforeUpdate.

Jim.
ASKER CERTIFIED SOLUTION
Avatar of PatHartman
PatHartman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also, if you use Subforms note that Access will always save the Parent data when you move into that subform. If you introduce the suggestions above, the user would be asked to save the data anytime they move into the subform (assuming they've "dirtied" the parent data, of course), so you could run into a situation where you must Delete the parent data if they decide to NOT save the Subform data. AFAIK, there's no way to avoid that other than using temporary tables to base your forms on.
Avatar of Murray Brown

ASKER

Thanks very much
You're welcome.