Cancel button doesn't delete the autosaved record from table

A

abrown

Greetings!

I have a form where users can search requests, as well as add new requests
bu clicking the "Add New Request" button. However, if the user decides not to
save the new request by clicking the "Save" button and wants to cancel by
clicking the "Main Menu" button, the request is still saved in the table. How
can I get this data to be deleted from the table?

Keep in mind that if the user is searching through the requests and clicks
the "Main Menu" button I don't want that data deleted...only when there is
being a new request entered and the user wants to cancel that new request.

Here is the code I currently have, which is not working properly.

Private Sub btnMainMenu_Click()
Dim saveRecord As Integer
saveRecord = MsgBox("Do you want to save your changes?", vbYesNo, "Save
Changes")
If saveRecord = vbYes Then
Me.btnSave.SetFocus
Else
MsgBox ("These changes have not been saved.")
DoCmd.CancelEvent
Me.Undo
DoCmd.Close
End If
End Sub
 
A

akphidelt

Try this

If MsgBox("Do you want to save your changes?", vbyesno,"Save Changes") =
vbYes Then

Me.btnSave.SetFocus

Else

Me.Undo
DoCmd.Close

MsgBox("These chnages have not been saved.")
End If
 
A

Allen Browne

The only way to do this is to move the code out of your button's Click
event, and into the BeforeUpate event procedure of the *form*.

That's the only way to catch all the ways the record can be saved, e.g. by
tabbing through all fields to the next record, using the navigation buttons,
the menu, the find dialog, applying a filter or a search, pressing
Shift+Enter, closing the form, closing Access, and so on.

Your button then merely forces the save, e.g.:
RunCommand acCmdSaveRecord
This triggers Form_BeforeUpdate, and so the code you moved to there runs.
 

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