I have some VBA I've written for my form, which prevents the record from saving until the user clicks the "Save" button on the form. If they just close the form, it will prompt them if they want to save with a vbYesNoCancel.
The cancel prompt DOES work...sort of. The problem is that if a user clicks "cancel" on the message box, they then get the error shown in the attached screenshot. Is there a way around this, or is this just one of those Access caveats?
The cancel prompt DOES work...sort of. The problem is that if a user clicks "cancel" on the message box, they then get the error shown in the attached screenshot. Is there a way around this, or is this just one of those Access caveats?
Code:
Option Compare Database
Private UserSave As Boolean
Private Sub Cancel_Btn_Click()
UserSave = True
If Me.Dirty = True Then
Me.Undo
End If
DoCmd.Close acForm, Me.Name, acSaveNo
End Sub
Private Sub Form_AfterInsert()
CurrentDb.Execute "UPDATE Customers SET Customers.CustomerNumber = 'CL-" & Format([CustomerID], "000000") & _
"' WHERE [CustomerID]= " & Me.CustomerID, dbFailOnError
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Not UserSave Then
If Me.Dirty = True Then
Dim UserResponse As Integer
UserResponse = MsgBox("Save before closing?", vbQuestion + vbYesNoCancel, "Test")
Select Case UserResponse
Case vbYes
Case vbNo
Me.Undo
Case Else
Cancel = True
End Select
End If
End If
End Sub
Private Sub Save_Btn_Click()
UserSave = True
If Me.Dirty = True Then
Me.Dirty = False
End If
DoCmd.Close acForm, Me.Name, acSaveYes
End Sub