FIX: Messagebox Shows in DDX_FieldCBString & DDX_FieldLBString

ID: Q143330


The information in this article applies to:
  • The Microsoft Foundation Classes (MFC), included with:
    • Microsoft Visual C++ 32-bit Edition, version 4.0


SYMPTOMS

When you map a control to a recordset variable by using DDX_FieldCBString or DDX_FieldLBString, the following message may appear in a messagebox when UpdateData() is called:

Please enter no more than 0 characters.


CAUSE

The DDX_FieldCBString and DDX_FieldLBString functions incorrectly check the length of the destination CString variable. The problem exists on lines 420 and 509 in Daoview.cpp. The code is performing the following check;


   if (nLen > value.GetAllocLength()) ... 
It is possible that the buffer for the string has been emptied by a call to AddNew() or SetFieldNull(). This would cause the CString value to be empty, which causes the messagebox to be displayed.


RESOLUTION

Create a new function by copying the DDX_FieldCBString or DDX_FieldLBString from Daoview.cpp located in the \Msdev\Mfc\Src directory. In the new function, add an additional parameter to be used as the maximum allowable length (the size of the field). You'll then need to replace the DDX call in your DoDataExchange() function with the new function you created.

For the DDX_FieldCBString function, change the function prototype to something like this:


   void DDX_MyFieldCBString(CDataExchange* pDX, int nIDC, CString& value,
   CDaoRecordset* pRecordset, int nMaxFieldLength) 
Replace these lines:

   if (nLen > value.GetAllocLength())
      AfxFailMaxChars(pDX, value.GetAllocLength());
   // get known length
   ::GetWindowText(hWndCtrl, value.GetBuffer(0), nLen+1); 
with these lines:

   if (nLen > nFieldLength)
      AfxFailMaxChars(pDX, nFieldLength);
   // get known length
   ::GetWindowText(hWndCtrl, value.GetBuffer(nLen), nLen+1); 
Note that GetBuffer() is no longer receiving 0 but is using the value in nLen.

For the DDX_FieldLBString() function, change the prototype to:

   void DDX_MyFieldLBString(CDataExchange* pDX, int nIDC, CString& value,
   CDaoRecordset* pRecordset, int MaxFieldLength) 
Replace these lines:

   if (nLen > value.GetAllocLength())
     AfxFailMaxChars(pDX, value.GetAllocLength());
   ::SendMessage(hWndCtrl, LB_GETTEXT, nIndex,
                 (LPARAM)(LPSTR)value.GetBuffer(0)); 
with these lines:

   if (nLen > nFieldLength)
     AfxFailMaxChars(pDX, nFieldLength);
   ::SendMessage(hWndCtrl, LB_GETTEXT, nIndex,
                (LPARAM)(LPSTR)value.GetBuffer(nLen)); 
You will need to include a prototype for AfxFailMaxChars() in your code. Here's the prototype:

   void _stdcall AfxFailMaxChars(CDataExchange* pDX, int nChars); 


STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This bug was corrected in Visual C++ 4.1.

Additional query words: kbVC400bug

Keywords : kbDAO kbDatabase kbMFC kbVC kbVC410fix
Version : winnt:
Platform : winnt
Issue type : kbbug


Last Reviewed: January 31, 2000
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.