CancelUpdate, Update Methods Example (MDB)

The following example creates a new record in an Employees table, enters values, and prompts the user to save the changes. If the user chooses not to save the changes, the Update method is canceled.

Sub NewRecord()
    Dim dbs As Database, rst As Recordset

    ' Return reference to current database.
    Set dbs = CurrentDb
    Set rst = dbs.OpenRecordset("Employees")
    With rst
        ' Add new record to end of Recordset object.
        .AddNew
        !LastName = "Christopher"        ' Add data.
        !FirstName = "Kevin"
    End With
    ' Prompt user to save changes.
    If MsgBox("Save these changes?", vbYesNo) = vbNo Then
        ' If user chooses No, cancel changes.
        rst.CancelUpdate
    Else
        ' If user chooses Yes, save changes.
        rst.Update
    End If
    rst.Close
    Set dbs = Nothing
End Sub