Microsoft KB Archive/150122

= PRB: CDaoRecordView Toolbar Buttons Malfunction =

Article ID: 150122

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 4.0 Standard Edition

 Microsoft Visual C++ 4.1 Subscription

 Microsoft Visual C++ 4.2 Enterprise Edition

 Microsoft Visual C++ 4.2 Professional Edition</li></ul>

 Microsoft Visual C++ 4.2 Enterprise Edition</li></ul>

 Microsoft Visual C++ 4.2 Professional Edition</li></ul>

 Microsoft Visual C++ 5.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 5.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 6.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q150122

<div class="symptoms_section">

SYMPTOMS
CDaoRecordView depends on an instance of CDaoRecordset to display data. The CDaoRecordView class also needs to stay in synchronization with the current record displayed by the CDaoRecordset instance. Calling the Seek, MoveFirst, MoveNext, or other methods directly on the CDaoRecordset being used by a CDaoRecordView causes the CDaoRecordView to behave incorrectly.

<div class="cause_section">

CAUSE
CDaoRecordView uses four member variables to keep track of bookkeeping information. The first three variables listed below store bookmarks for the first, last, and current record. The fourth variable is used to enable or disable the toolbar buttons corresponding to MoveFirst, MovePrev, MoveNext, and MoveLast. <pre class="fixed_text">  m_varBookmarkCurrent m_varBookmarkFirst m_varBookmarkLast m_nStatus These variables are only maintained when the CDaoRecordView::OnMove method is invoked. This method invokes the CDaoRecordset MoveFirst, MoveNext, MovePrevious, or MoveLast methods, and updates the variables. Only OnMove keeps these variables in synchronization.

The following list contains the more common CDaoRecordset methods that change the current record: <pre class="fixed_text">  - Find            - Move            - Requery - FindFirst      - MoveFirst       - Seek - FindLast       - MoveLast        - SetAbsolutePosition - FindNext       - MoveNext        - SetBookmark - FindPrev       - MovePrev        - SetPercentPosition When the AddNew method is called, a new record is added to the recordset but the current record bookmark does not change. To change the newly added record to the current record, use the following method: // set the current bookmark to the newly added record m_pSet->SetBookmark(GetLastModifiedBookmark); NOTE: GetLastModifiedBookmark needs to be called right after the call to Update to return the newly added record. Calling it after one of the recordview MoveXXXX functions returns a bookmark to the record that was current prior to the move operation.

<div class="resolution_section">

RESOLUTION
After performing a CDaoRecordset method for an instance utilized by CDaoRecordView, reset the CDaoRecordView's bookkeeping variables as shown in the following code: if (!m_pSet->Seek(...)) {    if( !m_pSet->IsEOF ) {        m_varBookmarkCurrent = 1L;   // <<- re-initialize m_varBookmarkFirst =        // <<- the bookmark m_varBookmarkLast = 0L;  // <<- member variables! // Enable toolbar buttons m_nStatus |= AFX_DAOVIEW_SCROLL_BACKWARD;// First & Prev buttons m_nStatus |= AFX_DAOVIEW_SCROLL_NEXT;   // next toolbar button m_nStatus |= AFX_DAOVIEW_SCROLL_LAST;   // last toolbar button }    else {       // Move to some valid record } }  else {    // Seek failed - move to a valid record }

<div class="status_section">

STATUS
This behavior is by design.

<div class="moreinformation_section">

Steps to Reproduce Behavior
<ol> Generate a default AppWizard application named SeekTest, and give it the following attributes:

 <li>Database View without File Support.</li> <li>Select a data source, using DAO and a Table recordset type.</li> <li>Open the Student Registration database (STDREG32.MDB) on the SECTION table.</li></ul> </li> <li>Using the Resource View, add several text boxes to the IDD_SEEKTEST_FORM dialog resource that appears. Use the CTRL-DBLCLK short-cut to bind columns in SECTION to the dialog resource.</li> <li> Add a button to the IDD_SEEKTEST_FORM resource. Using ClassWizard, add the following code: m_pSet->SetCurrentIndex( _T("PrimaryKey") );

COleVariant varCourse ( _T("MATH201"), VT_BSTRT ); COleVariant varSection( _T( "1"    ), VT_BSTRT );

if(! m_pSet->Seek( _T( "=" ), &varCourse, &varSection ) ) {         // Seek failed - need to move to a valid record m_pSet->MoveFirst; }     UpdateData( FALSE ); </li> <li>Compile, build, and run the program.</li> <li>Click the MoveNext (>) button and then the MovePrevious (<) button to disable the MoveFirst (|<) and MovePrevious (<) toolbar buttons.</li> <li>Click the Seek button. Note that the MoveFirst and MovePrevious buttons are still incorrectly disabled.</li></ol>

Adding the code to clear the bookmarks, and resetting the m_nStatus variable corrects the toolbar and prevents other problems.

<div class="references_section">