Deleting Records from the Employee Database

The OnEditDelete member function deletes records from the Employee database:

void CDAOEMPDoc::OnEditDelete() 
{
	//If table is empty, there is nothing to delete.
	if(m_bEmptyTable)
		return;

	//Delete method depends on the current mode.
	short nEditMode = m_cEmpRecordSet.GetEditMode();

	try
		{
		switch (nEditMode)
			{
			case dbEditNone: // Just delete it.
				{
				m_cEmpRecordSet.Delete();
				m_cEmpRecordSet.MoveFirst();
				break;
				}
	
			case dbEditInProgress: //Forget changes.
				{
				m_cEmpRecordSet.CancelUpdate();
				m_cEmpRecordSet.Delete();
				m_cEmpRecordSet.MoveFirst();
				break;
				}

			case dbEditAdd: //If new record, go back to last known one.
				{
				m_cEmpRecordSet.CancelUpdate();
				m_cEmpRecordSet.SetBookmark(m_cLastGoodRecord);
				}
			}

		UpdateAllViews(NULL);
		}

	catch (CdbException e)
		{
		CdbLastOLEError exError;
		TCHAR szBuf[256];

		wsprintf(szBuf, _T("Error 0x%lx : %s\n"), e.m_hr, (LPCTSTR) exError.GetDescription());
		AfxMessageBox(szBuf);
		}
}

The behavior of this member function depends on whether the current record is new or has been edited. The OnEditDelete function uses case statement logic based on the value returned by the GetEditMode method. Note that dbDAO conveniently defines the same data access constants as Visual Basic DAO, such as dbEditNone. Depending on this value, the OnEditDelete function behaves as follows:

In all three cases, it is necessary to update the view by calling the UpdateAllViews function.