Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

closing a form without saving bound control data 2

Status
Not open for further replies.

shallman

Technical User
Jul 21, 2003
22
0
0
US
This is similiar to a previous post.

I have a form bound with controls to a record in a table. I have two buttons on the bottom of the form, "Save" and "Cancel". I want the user to be able to change data and then either save and close the form, or abort and close the form without updating the underlying table.

The "Save" is easy, simply run a macro to close the form on click, but I don't know the easiest way to abort the updates and close the form when "Cancel" is clicked.

Can anyone help?

Thanks,
SHallman
 
Use the following in your Cancel Click event procedure:

DoCmd.RunCommand acCmdUndo


This cancels the changes to the form before a save would take place.

Bob Scriver
Want the best answers? See FAQ181-2886
Nobody believes the official spokesman... but everybody trusts an unidentified source.
Author, Bagdad Bob???

 
This works, thanks!
The only problem I see now is that if no changes were made to the form, clicking on "Cancel" button produces the VW error:

Run-time error '2046':
The command or action 'Undo' isn't available now.

If changes were made to the form, it works great.
How would I check before running the DoCmd.RunCommand acCmdUndo command.

Thanks,
SHallman
 
I got it figured out. I put in an error handler:

On Error GoTo errorhandler
...
errorhandler:
DoCmd.Close

Thanks for the help!
 
I would set a status text box with a value of 1 as default. Set the each control properties of dirty to something like:

If Me!Control = 1 Then Me!Control = Me!Control + 1

Then on your cancel button do a case statement

Case Select Me!Control
Case 1
DoCmd.Close blah blah blah

Case Else

DoCmd.RunCommand acCmdUndo
DoCmd.Close Blah Blah Blah

End Select
 
Oh and if you dont want that "status" Control visible when the form is open in the properties:Format Set visible to false
 
You really only need to check the Me.dirty property of the form to determine if any of the controls have been updated. Put this in your Cancel button"

If Me.Dirty then
DoCmd.RunCommand acCmdUndo
end if

Now I usually don't allow the Cancel button to be enabled until after a control on the form has been updated. This can be done in the AfterUpdate event procedure:

If Me.Dirty then
Me![cmdCancel].enabled = True
End If

This requires that you toggle the .enabled property of the button back to False when a new record is selected or added. Then the above code will trigger to True as soon as a control is updated with data and tabbed to the next control.

Bob Scriver
Want the best answers? See FAQ181-2886
Nobody believes the official spokesman... but everybody trusts an unidentified source.
Author, Bagdad Bob???

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top