CRecordset::AddNew

virtual void AddNew( );
throw( CDBException );

Remarks

Call this member function to prepare for adding a new record to the table. You must call the Requery member function to see the newly added record. The record’s fields are initially Null. (In database terminology, Null means “having no value” and is not the same as NULL in C++.) To complete the operation, you must call the Update member function. Update saves your changes to the data source.

Note   If you have implemented bulk row fetching, you cannot call AddNew. This will result in a failed assertion. Although class CRecordset does not provide a mechanism for updating bulk rows of data, you can write your own functions by using the ODBC API function SQLSetPos. For an example of how to do this, see the sample DBFETCH. For more information about bulk row fetching, see the article Recordset: Fetching Records in Bulk (ODBC) in Visual C++ Programmer’s Guide.

AddNew prepares a new, empty record using the recordset’s field data members. After you call AddNew, set the values you want in the recordset’s field data members. (You do not have to call the Edit member function for this purpose; use Edit only for existing records.) When you subsequently call Update, changed values in the field data members are saved on the data source.

Caution   If you scroll to a new record before you call Update, the new record is lost, and no warning is given.

If the data source supports transactions, you can make your AddNew call part of a transaction. For more information about transactions, see class CDatabase. Note that you should call CDatabase::BeginTrans before calling AddNew.

Important   For dynasets, new records are added to the recordset as the last record. Added records are not added to snapshots — you must call Requery to refresh the recordset.

It is illegal to call AddNew for a recordset whose Open member function has not been called. A CDBException is thrown if you call AddNew for a recordset that cannot be appended to. You can determine whether the recordset is updatable by calling CanAppend.

For more information, see the following articles in Visual C++ Programmer’s Guide: Recordset: How Recordsets Update Records (ODBC), Recordset: Adding, Updating, and Deleting Records (ODBC), and Transaction (ODBC).

Example

See the article Transaction: Performing a Transaction in a Recordset (ODBC) in Visual C++ Programmer’s Guide.

CRecordset OverviewClass MembersHierarchy Chart

See Also   CRecordset::Edit, CRecordset::Delete, CRecordset::Update, CRecordset::Requery, CDatabase::BeginTrans, CDBException