Note You should have completed the topics Adding and Deleting Records through Implementing the Add Command in Lesson 4 of the Enroll tutorial before starting this topic.
Add mode is completed when the user moves off the record. DaoEnrol implements this by overriding the CDaoRecordView::OnMove member function.
To implement Add functionality in the OnMove function override
CSectionForm
.OnMove
.OnMove
function, and replace the highlighted //TODO comment with the following code: if ( m_bAddMode )
{
if ( !UpdateData( ) )
return FALSE;
try
{
m_pSet->Update( );
}
catch( CDaoException* e )
{
AfxMessageBox( e->
m_pErrorInfo->m_strDescription );
e->Delete( );
return FALSE;
}
m_pSet->Requery( );
UpdateData( FALSE );
m_ctlSection.SetReadOnly( TRUE );
m_bAddMode = FALSE;
return TRUE;
}
else
{
return CDaoRecordView::OnMove( nIDMoveCommand );
}
Error handling in the DAO database classes differs from that of the ODBC database classes. The implementation of else in this code block requires a message box with error information to complete the if/else control structure.
In its default CDaoRecordView implementation, OnMove moves to the next, previous, first, or last record. If the application has changed the recordset field data members for the current record before the move, the framework updates the data source before moving to another record.
Normally, the Move commands behave as you might expect: MoveNext moves to the next record, and so on. But as a consequence of the decision to Requery during the Add operation, when the user chooses any move command when adding a record, DaoEnrol always effectively moves to the first record. That’s because requerying the recordset automatically sets the recordset to the first record.