I have a cancel button on an add new record form and its not working for some reason. When I press cancel it prompts if I really want to cancel and when I press yes it cancels the record creation BUT it adds a number to the recordID autonumber as if one has been created. Is there anyway to stop this? Here is my code
Option Compare Database
Private Sub Cancel_Click()
On Error GoTo Err_Cancel_Click
If Me.Dirty = True Then
If MsgBox("Are you sure you wish to Cancel? Any changes you made will be lost. Please click Yes to cancel or No to return to the form", vbYesNo) = vbYes Then
'If yes then undo unsaved changes:
DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
'Then close form:
DoCmd.Close
End If
End If
If Me.Dirty = False Then
'If no changes close form:
DoCmd.Close
End If
Exit_Cancel_Click:
Exit Sub
Err_Cancel_Click:
Resume Exit_Cancel_Click
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
'If the form data has changed a message is shown asking if
'the changes should be saved. If the answer is no then
'the changes are undone
On Error GoTo BeforeUpdate_Error
If Me.Dirty Then
'if record has been changed the dirty property
' is set to true Display message to save the record
If MsgBox("Are you sure you want to save your changes?", _
vbYesNo + vbQuestion, "Save Changes") = vbNo Then
Me.Undo
End If
End If
BeforeUpdate_Exit:
Exit Sub
BeforeUpdate_Error:
MsgBox Err.Description
Resume BeforeUpdate_Exit
End Sub
End Sub