![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg3nhN2fyM8hAFoMwCx5ezirtmo-n93iLBMsO-OuwMRrMur5nLQuMDv7x2JcJ4fqucb6WiAOOt75POVRqMr2hvBSw-baNMHs3c23vnhKLKWr5vhxKORa-ZLYd8MOyxqkz-xlL6x/s280/dnd_ActiveX_Reference.JPG)
How to update an Access Table using ADO
The following code snippet would be helpful to update an Access 2007 database table using VBA. The code uses ADO and requires a reference to ActiveX Data Objects Library
The sample uses a simple table which contains a name and a location field.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpeJ1ZCipXO51VgW3Zs7T4UBfcuCI2oZ7ALC5tf8XtipSR3hKmHhWdrhlPNHcllfhuCbHc8cdGEvcGplCe4A0r8l9dJ8OCdJNpzkj3UHu_G_Hz6HlPnICw8wjlhf0DHaRN0czb/s280/dnd_SampleDatabase_DesignView.JPG)
The code uses the SQL update query to update the database. The query is executed by the ADO’s command execute method
Sub Simple_SQL_Update_Data()
Dim Cn As ADODB.Connection '* Connection String
Dim oCm As ADODB.Command '* Command Object
Dim sName As String
Dim sLocation As String
Dim iRecAffected As Integer
On Error GoTo ADO_ERROR
Set Cn = New ADODB.Connection
Cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\comp\Documents\SampleDB.accdb;Persist Security Info=False"
Cn.ConnectionTimeout = 40
Cn.Open
sName = "Krishna Vepakomma"
sLocation = "Cincinnati, OH"
Set oCm = New ADODB.Command
oCm.ActiveConnection = Cn
oCm.CommandText = "Update SampleTable Set Location ='" & sLocation & "' where UserName='" & sName & "'"
oCm.Execute iRecAffected
If iRecAffected = 0 Then
MsgBox "No records inserted"
End If
If Cn.State <> adStateClosed Then
Cn.Close
End If
Application.StatusBar = False
If Not oCm Is Nothing Then Set oCm = Nothing
If Not Cn Is Nothing Then Set Cn = Nothing
ADO_ERROR:
If Err <> 0 Then
Debug.Assert Err = 0
MsgBox Err.Description
Err.Clear
Resume Next
End If
End Sub
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuVnd5VYTqIYM6zw8KQCq2j5VvFCUEU9bk0powf1Qrg-mSjagsszHu_F-faK62oQ7hrpDh2ZduE2EbmloRS3Ev-39bK-iKDpwCTh9ekEmLW8-ENYaRgHz-376M3pYFPkQuingf/s280/dnd_SampleDatabase_DataView.JPG)
Table Before Update Command
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhwxQMiQg8S0f662VXysXBl7qSirHvv8lDsDV1v9Ysz9vXjM_lLEYUIqsxwrlLZW_PyJ9QSI4xuPcOVmGQi8a34NlwxI8pIGi-JqoySPXQCu89KekYW7kBCx9UBZhtXOs8VBB_K/s280/dnd_SampleDatabase_DataViewUpdated.JPG)
Table After Update Command