Name

recordset.AddNew([FieldList], [Values])

Synopsis

Creates a new record.

Argument

Description

FieldList

A single field name or an array of names or ordinal numbers specifying the fields in the new record

Values

A single field value or an array of values for the fields

The following code adds a new record to the Employees table in the Northwind Traders sample database using cell values on the current worksheet:

Dim cnn As ADODB.Connection
Dim cmd As ADODB.Command
Dim rs As ADODB.Recordset
Dim strDbPath As String
Dim strConnect As String
 
Set cnn = New ADODB.Connection
Set cmd = New ADODB.Command
Set rs = New ADODB.Recordset
strDbPath = "C:\Program Files\Microsoft Office\OFFICE11\SAMPLES\Northwind.mdb"
cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" _
    & "Data Source=" & strDbPath
 
cnn.Open
rs.Open "Employees", cnn, adOpenDynamic, adLockOptimistic, adCmdTable
 
rs.AddNew
rs!LastName = ActiveSheet.Range("B4")
rs!FirstName = ActiveSheet.Range("C4")
rs.Update
 
rs.Close
cnn.Close
Set rs = Nothing
Set cnn = Nothing

Get Programming Excel with VBA and .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.