| FIX: Incorrect Return Value From CRecordset::IsOpen()Last reviewed: September 18, 1997Article ID: Q120206 | 
| 1.50 1.51 1.52 1.52b
WINDOWS
kbprg kbfixlist kbbuglist The information in this article applies to: 
 
 SYMPTOMSWhen CRecordset::IsOpen() is called, it returns TRUE even if the CRecordset has not been opened. This happens only if a CRecordset is constructed with a CDatabase pointer being passed to it. 
 CAUSECRecordset::IsOpen() checks whether the statement handle of the recordset has been allocated as follows: _AFXDBCORE_INLINE BOOL CRecordset::IsOpen() const      { ASSERT_VALID(this); return m_hstmt != SQL_NULL_HSTMT; }
The statement handle is usually allocated when CRecordset::Open() is called
(that is, when a recordset is opened).But, if a CRecordset is constructed with a CDatabase pointer being passed to it, the CRecordset constructor allocates a statement handle and assigns a value to the member variable m_hstmt. So, if a call to CRecordset::IsOpen() is made after creating a CRecordset in this fashion, it returns TRUE even if the recordset is not open. 
 RESOLUTIONTo work around this problem, use one of the following methods: 
 Create a Custom IsOpen Routine
 Use a Flag to Track Whether the Recordset Has Been OpenedDerive a class from CRecordset and create a new member variable that would be set whether the recordset has been opened. Override CRecordset::Open and set the flag in the function. Create a CRecordset::IsMyOpen that would check whether the flag is set. Then call IsMyOpen instead of IsOpen to check whether the recordset has been opened. 
 STATUSMicrosoft has confirmed this to be a bug in the products listed at the beginning of this article. This bug was corrected in the 32-bit version of MFC. 
 MORE INFORMATION
 Sample Code
    BOOL CMyRecordset::IsMyOpen() const
   {
      if (m_hstmt == NULL)
         return FALSE;
      RETCODE nRetCode;
      SWORD nCols;
      AFX_SQL_SYNC(::SQLNumResultCols(m_hstmt, &nCols));
      if (nRetCode == SQL_ERROR)
      {
         TRACE0("Error: SQLNumResultCols failed during IsOpen().\n");
         return FALSE;
      }
      else if (nCols == 0)
         return FALSE;
      return TRUE;
   }
 | 
| Additional reference words: 1.50 1.51 1.52 2.50 2.51 2.52 2.53 
 © 1998 Microsoft Corporation. All rights reserved. Terms of Use. |