Reputation: 1212
I've been working in Access on a project and the longer I work on it the more disdain I have for it. I've been googling and attempting workarounds all day but I can't quite find what I am trying to do.
I have a form that is linked to a table. I have one button that will set the record of the form to a new record. If you start typing into any one of the text boxes the record starts updating, it's not really doing an insert at this point. However if I navigate off the record the update is called. there isn't anything I can do to stop the update from happening if the user does not want to add a new record that I can find. I'm using
DoCmd.GoToRecord , , acNewRec
To get the form to have all the text boxes go to a new record. At this point as soon as you start typing into any of the text boxes you can't go back. The autonumber will incriment and I have sku builder that concatenates the autoNumber to a prefix to act as a serial number.
What I need to do, is figure out a way to get the update to cancel if the user clicks close. I've tried to disable close, but you can always right click the bar and select close from there. This, along with anything that prompts the form to close commits that in process record and subsequently fires the "afterUpdate" event. I read some Microsoft documentation and "form_unload" is called first in the workflow but insert always goes before that. I've also tried to use the "beforeUpdate" but that fires as soon as you attempt to write into one of the boxes and not right before "afterUpdate"
It seems there is a pretty huge hole in the space between beforeUpdate and afterUpdate. I don't think this is a terribly complicated operation I'm trying to attempt here but perhaps I've been working on it for so long I have missed a simpler solution. I just need to cancel the insert operation or prompt the user that they are closing the form with an open record. If they hit no insert will fire, and yes it will.
Also, I have no appetite to rewrite for ADO because of my timeline. I just need to quickly throw some forms together and get it done. There are also a ton of fields that I don't want to setup for an ADO insert. They are all built but I have this one issue that is giving me grief and I need to apply this solution to all the forms.
TLDR - Stopping a databound form from inserting if the user has entered text and tries to exit the form.
Solution Edit: Change the form to modal/popup and disable close. Add a button to handle close. Add boolean flag
Private Sub Form_BeforeInsert(Cancel As Integer)
NR = True
End Sub
Then add the code for the close button.
Private Sub cmdClose_Click()
Dim msgRes As VbMsgBoxResult
If NR Then
msgRes = MsgBox("Do you want to save the current new record?", vbYesNoCancel, "Closing form...")
If msgRes = vbYes Then
DoCmd.RunCommand acCmdSaveRecord
DoCmd.Close
ElseIf msgRes = vbNo Then
Me.Undo
DoCmd.Close
End If
End If
DoCmd.Close
End Sub
Upvotes: 3
Views: 5103
Reputation: 12739
You can always do either a Me.Undo
or DoCmd.RunCommand acCmdUndo
on FormClose
OR if you still can't get it to work (I've spent a lot of time in Access and I know it can be finicky) you can do a DoCmd.RunSql ("Delete * from TableName where Id=NewID")
You can also try this:
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.Close , , acSaveNo
You may also want to disable the close button and create a button to cancel the record so you can put all the code in there. If you set the form mode to a dialog you shouldn't be able to right click close it.
Upvotes: 3