I have one last question for the moment for my class database. For certain forms where a user can view data, I want them to be able to close the form and not be prompted if they did not make any changes. HOWEVER, if they chose to change some data in the form, say to edit a record, I then want them to get a pop-up box prompting the user if they want to save the change. If they select yes, then the old record is overwritten and the change is saved to the record, but if they select no, then the record isn't changed at all and the form closes.
How can I do this? I'm using Access 2016, and what I've tried so far results in errors. Here are 2 methods I've tried in the forms BeforeUpdate event in VBA.
Method 1:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
Dim iResponse As Integer
' Specify the message to display.
strMsg = "Do you wish to save the changes?" & Chr(10)
strMsg = strMsg & "Click Yes to Save or No to Discard changes."
' Display the message box.
iResponse = MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?")
' Check the user's response.
If iResponse = vbNo Then
' Undo the change.
DoCmd.RunCommand acCmdUndo
' Cancel the update.
Cancel = True
End If
End Sub
Error - "The expression Before Update you entered as the event property setting produced the following error: A problem occured while Microsoft Access was communicated with the OLE server or ActiveX Control."
Method 2:
Private Sub Form_BeforeUpdate(Cancel As Integer)
'Provide the user with the option to save/undo
'changes made to the record in the form
If MsgBox("Changes have been made to this record." _
& vbCrLf & vbCrLf & "Do you want to save these changes?" _
, vbYesNo, "Changes Made...") = vbYes Then
DoCmd.Save
Else
DoCmd.RunCommand acCmdUndo
End If
End Sub
Error - Same as above.
Any help is greatly appreciated!
Thanks!