--- title: "Update and CancelUpdate Methods Example (VB) | Microsoft Docs" ms.prod: sql ms.prod_service: connectivity ms.technology: connectivity ms.custom: "" ms.date: "01/19/2017" ms.reviewer: "" ms.topic: conceptual dev_langs: - "VB" helpviewer_keywords: - "CancelUpdate method [ADO]" - "Update method [ADO], Visual Basic example" ms.assetid: 55bedd08-7440-4da4-b854-4ac9ef2fdedb author: MightyPen ms.author: genemi --- # Update and CancelUpdate Methods Example (VB) This example demonstrates the [Update](../../../ado/reference/ado-api/update-method.md) method in conjunction with the [CancelUpdate](../../../ado/reference/ado-api/cancelupdate-method-ado.md) method. ``` 'BeginUpdateVB Public Sub Main() On Error GoTo ErrorHandler 'To integrate this code 'replace the data source and initial catalog values 'in the connection string ' recordset and connection variables Dim rstEmployees As ADODB.Recordset Dim Cnxn As ADODB.Connection Dim strCnxn As String Dim strSQLEmployees As String ' buffer variables Dim strOldFirst As String Dim strOldLast As String Dim strMessage As String ' Open connection Set Cnxn = New ADODB.Connection strCnxn = "Provider='sqloledb';Data Source='MySqlServer';" & _ "Initial Catalog='Pubs';Integrated Security='SSPI';" Cnxn.Open strCnxn ' Open recordset to enable changes Set rstEmployees = New ADODB.Recordset strSQLEmployees = "SELECT fname, lname FROM Employee ORDER BY lname" rstEmployees.Open strSQLEmployees, Cnxn, adOpenKeyset, adLockOptimistic, adCmdText ' Store original data strOldFirst = rstEmployees!fname strOldLast = rstEmployees!lname ' Change data in edit buffer rstEmployees!fname = "Linda" rstEmployees!lname = "Kobara" ' Show contents of buffer and get user input strMessage = "Edit in progress:" & vbCr & _ " Original data = " & strOldFirst & " " & _ strOldLast & vbCr & " Data in buffer = " & _ rstEmployees!fname & " " & rstEmployees!lname & vbCr & vbCr & _ "Use Update to replace the original data with " & _ "the buffered data in the Recordset?" If MsgBox(strMessage, vbYesNo) = vbYes Then rstEmployees.Update Else rstEmployees.CancelUpdate End If ' show the resulting data MsgBox "Data in recordset = " & rstEmployees!fname & " " & _ rstEmployees!lname ' restore original data because this is a demonstration If Not (strOldFirst = rstEmployees!fname And _ strOldLast = rstEmployees!lname) Then rstEmployees!fname = strOldFirst rstEmployees!lname = strOldLast rstEmployees.Update End If ' clean up rstEmployees.Close Cnxn.Close Set rstEmployees = Nothing Set Cnxn = Nothing Exit Sub ErrorHandler: ' clean up If Not rstEmployees Is Nothing Then If rstEmployees.State = adStateOpen Then rstEmployees.Close End If Set rstEmployees = Nothing If Not Cnxn Is Nothing Then If Cnxn.State = adStateOpen Then Cnxn.Close End If Set Cnxn = Nothing If Err <> 0 Then MsgBox Err.Source & "-->" & Err.Description, , "Error" End If End Sub ' EndUpdateVB ``` This example demonstrates the **Update** method in conjunction with the [AddNew](../../../ado/reference/ado-api/addnew-method-ado.md) method. ``` Attribute VB_Name = "Update" ``` ## See Also [CancelUpdate Method (ADO)](../../../ado/reference/ado-api/cancelupdate-method-ado.md) [Recordset Object (ADO)](../../../ado/reference/ado-api/recordset-object-ado.md) [Update Method](../../../ado/reference/ado-api/update-method.md)