Implementing the Delete Command in DaoEnrol

In response to a Delete command, the record view deletes the current record by calling the Delete member function of its associated recordset.

Suggested Reading in the Visual C++ User’s Guide

To implement the Delete command

  1. Use ClassView to jump to the OnRecordDelete starter function in class CSectionForm.

  2. Implement the handler with the following code:

    try
    {
    m_pSet->Delete();
    }
    catch(CDaoException* e)
    {
    AfxMessageBox(e->
    m_pErrorInfo->m_strDescription);
    e->Delete();
    }
    
    // Move to the next record after the one just deleted
    m_pSet->MoveNext();
    
    // If we moved off the end of file, move back to last record
    if (m_pSet->IsEOF())
    m_pSet->MoveLast();
    
    // If the recordset is now empty, clear the fields left over
    // from the deleted record
    if (m_pSet->IsBOF())
    m_pSet->SetFieldNull(NULL);
    UpdateData(FALSE);
    

Catch any exceptions thrown by the recordset’s Delete function so that errors are reported to the user. The CDaoException data member m_pErrorInfo retrieves fairly user-friendly error messages prepared by the underlying CDAOException object.

For the DaoEnrol sample and tutorial, the decision was made to move to the record following the deleted record. You could move to the previous record after a delete operation or anywhere else as long as you, or the user, moves off the deleted record.