AddNew Method

See Also    Example    Applies To

Creates a new record for an updatable CdbRecordset object.

Syntax

VOIDAddNew(VOID);

Remarks

Use the AddNew method to create and add a new record in the CdbRecordset object named by recordset. This method sets the fields to default values, and if no default values are specified, it sets the fields to Null (the default values specified for a table-type CdbRecordset).

After you modify the new record, use the Update method to save the changes and add the record to the CdbRecordset. No changes occur in the database until you use the Update method.

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

Note When you use AddNew in a Microsoft Jet workspace and the database engine has to create a new 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 have not moved to the last record of your CdbRecordset, records added to base tables by other processes may be included if they are positioned beyond the current record. If you add a record to your own CdbRecordset, however, the record is visible in the CdbRecordset and included in the underlying table where it becomes visible to any new CdbRecordset objects.

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

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.

Note To add, edit, or delete a record, there must be a unique index on the record in the underlying data source. If not, a "Permission denied" error will occur on the AddNew, Delete, or Edit method call in a Microsoft Jet workspace, or an "Invalid argument" error will occur on the Update call in an ODBCDirect workspace.

Usage

#include <afxole.h>
#include <dbdao.h>

CdbRecordset      rst;
...
rst.AddNew();      // Create an empty recordset.
...            // Set the values of the recordset.
//fields.
rst.Update()      // Save your changes.
rst.Close();      // Close the recordset.