>

AddNew Method

Applies To

Dynaset-Type Recordset Object, Recordset Object, Table-Type Recordset Object.

Description

Creates a new record for a table-type or dynaset-type Recordset object.

Syntax

object.AddNew

The object placeholder represents the name of an open table-type or dynaset-type Recordset object to which you want to add a new record.

Remarks

The AddNew method creates a new record you can edit and add to the Recordset object named by object. This method sets the fields to Null (the default values specified for a table-type Recordset) or default values, if any.

After you modify the new record, use the Update method to save the changes and add the record to the recordset. No changes are made to the database until you use the Update method.

Caution

If you issue an AddNew and then perform any operation that moves to another record without using Update, your changes are lost without warning. In addition, if you close the object or end the procedure that declares the object or its Database object, the new record and the changes made to it are discarded without warning.

When you use AddNew and the Microsoft Jet database engine has to create a new 2-kilobyte page to hold the current record, page locking is pessimistic. If the new record fits in an existing page, page locking is optimistic.

If you haven't moved to the last record of your dynaset, records added to base tables may be included if they would be positioned beyond the current record. If you add a record to a dynaset-type Recordset using the AddNew method, however, the record is visible in the Recordset and included in the underlying table where it becomes visible to any new Recordset objects.

The position of the new record depends on the type of Recordset:

The record that was current before you used AddNew remains current. If you want to make the new record current, you can set the Bookmark property to the bookmark identified by the LastModified property setting.

See Also

Bookmark Property; Delete Method; Index Object; LastModified Property; Move Method; MoveFirst, MoveLast, MoveNext, MovePrevious Methods; Seek Method.

Specifics (Microsoft Access)

When you use a bookmark in a Microsoft Access module, you must include an Option Compare Binary statement in the Declarations section of the module. A bookmark is a Variant array of Byte data, so the string comparison method for the module must be binary. If a bookmark is evaluated with a text-based string comparison method, such as the Option Compare Text statement or the default setting for the Option Compare Database statement, the current record may be set to an incorrect record.

Example

This example creates a new record in a table named Customers, enters values, saves the changes, and then moves to and deletes the new record.


Dim dbsNorthwind As Database, rstCustomers As Recordset
Set dbsNorthwind = DBEngine.Workspaces(0).OpenDatabase("Northwind.mdb")
' Open table.
Set rstCustomers = dbsNorthwind.OpenRecordset("Customers")
With rstCustomers
    .AddNew    ' Create new record.
    !CustomerID = "FINNF"    ' Set record key.
    !CompanyName = "Finnegan's Foods"    ' Set company name.
    .Update    ' Save changes.
    .Bookmark = rstCustomers.LastModified    ' Go to new record 
    .Delete    ' Delete new record.
    .Close    ' Close Table.
End With
dbsNorthwind.Close    ' Close Database.
Example (Microsoft Access)

The following example creates a new record in an Employees table and saves the changes.


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


    ' Return Database variable pointing to current database.
    Set dbs = CurrentDb
    Set rst = dbs.OpenRecordset("Employees")
    With rst
        ' Add new record to end of Recordset object.
        .AddNew
        .[LastName] = "Russell"    ' Add data.
        .[FirstName] = "Peter"
        .Update            ' Save changes.
    End With
    dbs.Close
End Sub