CancelUpdate Method Example

This example shows how the CancelUpdate method is used with the AddNew method.

Sub CancelUpdateX()

    Dim dbsNorthwind As Database
    Dim rstEmployees As Recordset
    Dim intCommand As Integer

    Set dbsNorthwind = OpenDatabase("Northwind.mdb")
    Set rstEmployees = dbsNorthwind.OpenRecordset( _
        "Employees", dbOpenDynaset)

    With rstEmployees
        .AddNew
        !FirstName = "Kimberly"
        !LastName = "Bowen"
        intCommand = MsgBox("Add new record for " & _
            !FirstName & " " & !LastName & "?", vbYesNo)
        If intCommand = vbYes Then
            .Update
            MsgBox "Record added."
            ' Delete new record because this is a 
            ' demonstration.
            .Bookmark = .LastModified
            .Delete
        Else
            .CancelUpdate
            MsgBox "Record not added."
        End If
    End With

    dbsNorthwind.Close

End Sub

This example shows how the CancelUpdate method is used with the Edit method.

Sub CancelUpdateX2()

    Dim dbsNorthwind As Database
    Dim rstEmployees As Recordset
    Dim strFirst As String
    Dim strLast As String
    Dim intCommand As Integer

    Set dbsNorthwind = OpenDatabase("Northwind.mdb")
    Set rstEmployees = dbsNorthwind.OpenRecordset( _
        "Employees", dbOpenDynaset)

    With rstEmployees
        strFirst = !FirstName
        strLast = !LastName
        .Edit
        !FirstName = "Cora"
        !LastName = "Edmonds"
        intCommand = MsgBox("Replace current name with " & _
            !FirstName & " " & !LastName & "?", vbYesNo)
        If intCommand = vbYes Then
            .Update
            MsgBox "Record modified."
            ' Restore data because this is a demonstration.
            .Bookmark = .LastModified
            .Edit
            !FirstName = strFirst
            !LastName = strLast
            .Update
        Else
            .CancelUpdate
            MsgBox "Record not modified."
        End If
        .Close
    End With

    dbsNorthwind.Close

End Sub