The OnEditAdd member function adds records to the Employee database:
void CDAOEMPDoc::OnEditAdd() { //If the table is empty, add a blank record. if(m_bEmptyTable) { m_cEmpRecordSet.AddNew(); m_cEmpRecordSet.Update(); m_cEmpRecordSet.MoveFirst(); m_bEmptyTable = FALSE; return; } else { if(!OKToMove()) return; //Remember the location before adding in case the user //cancels and wants to return there. m_cLastGoodRecord = m_cEmpRecordSet.GetBookmark(); m_cEmpRecordSet.AddNew(); m_cEmpRecordSet.Fields[EMP_HIRE_DATE].SetValue(COleVariant(32874L)); // Default to 1/1/90 } UpdateAllViews(NULL); }
As with any attempt to reposition within the recordset, the OKToMove function is called first to check whether or not the current record has been edited by the user. Before adding the new record, the current position is saved by using a bookmark. This is done with the GetBookmark method as in Visual Basic. The bookmark itself is typed as CdbBookmark in dbDAO rather than as a Variant as in Visual Basic. The fact that the type is different is not important because the value of the bookmark itself is meaningless. The bookmark is merely used for returning to a record. If the user deletes the new record, the bookmark is used to move back to the previous position.