Implementing the Delete Command

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++ Programmer’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(CDBException, e)
    {
    AfxMessageBox(e->m_strError);
    return;
    }
    END_CATCH
    
    // 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 CDBException data member m_strError is a fairly user-friendly error message, prepared by the underlying ODBC driver.

If you want to customize the error message, you can force the error condition, then examine m_strStateNativeOrigin for a particular state or native value. You can look up error messages in the ODBC Programmer’s Reference, Appendix A, ODBC Error Codes. Enroll takes the easy approach by displaying m_strError.

For Enroll, the decision was 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.