Delete Method

See Also    Example    Applies To

Syntax: Recordset

VOIDDelete(VOID);

Syntax: Collections

VOIDcollection.Delete (LPCTSTR pstr );

The collections Delete method syntax has these parts.

Part Description
collection An object variable that represents any collection that can accept new objects (for limitations, see the table at the end of this topic).
pstr A pointer to a string which names the element of the collection to be deleted.

Remarks

You can use the Delete method to delete a current record from a CdbRecordset or a member from a collection, such as a stored table from a database, a stored field from a table, or a stored index from a table.

Recordsets

A Recordset must contain a current record before you use Delete; otherwise, a run-time error occurs.

In an updatable Recordset object, Delete removes the current record and makes it inaccessible. Although you can't edit or use the deleted record, it remains current. Once you move to another record, however, you can't make the deleted record current again. Subsequent references to a deleted record in a Recordset are invalid and produce an error.

You can undo a record deletion if you use transactions and the Rollback method.

If the base table is the primary table in a cascading delete relationship, deleting the current record may also delete one or more records in a foreign table.

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 method call in an ODBCDirect workspace.

Collections

You can use the Delete method to delete a persistent object. However, if the collection is a Databases, Recordsets, or Workspaces collection (each of which is stored only in memory), you can remove an open or active object only by closing that object with the Close method.

The deletion of a stored object occurs immediately, but you should use the Refresh method on any other collections that may be affected by changes to the database structure.

When you delete a CdbTableDef object from the TableDefs collection, you delete the table definition and the data in the table.

The following table lists some limitations of the Delete method. The object in the first column contains the collection in the second column. The third column indicates if you can delete an object from that collection (for example, you can never delete a CdbContainer object from the Containers collection of a CdbDatabase object).

Object Collection Can you use the Delete method?
CdbDBEngine Workspaces No. Closing the objects deletes them.
CdbDBEngine Errors No
CdbWorkspace Connections No. Closing the objects deletes them.
CdbWorkspace Databases No. Closing the objects deletes them.
CdbWorkspace Groups Yes
CdbWorkspace Users Yes
CdbConnection QueryDefs No
CdbConnection Recordsets No. Closing the objects deletes them.
CdbDatabase Containers No
CdbDatabase QueryDefs Yes
CdbDatabase Recordsets No. Closing the objects deletes them.
CdbDatabase Relations Yes
CdbDatabase TableDefs Yes
CdbGroup Users Yes
CdbUser Groups Yes
CdbContainer Documents No
CdbQueryDef Fields No
CdbQueryDef Parameters No
CdbRecordset Fields No
CdbRelation Fields Only when the CdbRelation object is a new, unappended object.
CdbTableDef Fields Only when the CdbTableDef object is new and hasn’t been appended to the database, or when the Updatable property of the CdbTableDef is set to True.
CdbTableDef Indexes Only when the CdbTableDef object is new and hasn’t been appended to the database, or when the Updatable property of the CdbTableDef is set to True.
CdbIndex Fields Only when the CdbIndex object is new and hasn’t been appended to the database.
CdbDatabase, CdbField, CdbIndex, CdbQueryDef, CdbTableDef Properties Only when the property is user-defined.
CdbDBEngine, CdbParameter, CdbRecordset, Workspace Properties No

Usage

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

CdbRecordset      rst;
...
rst.MoveLast();         // Make the last record the current record.
rst.Delete();            // Delete the current record.
rst.Close();            // Close the recordset.