Article ID: 140599
Article Last Modified on 11/21/2006
COleVariant varFieldValue;
rs.AddNew();
varFieldValue = _T("MSFT");
rs.SetFieldValue(_T("Customer_ID"), varFieldValue);
varFieldValue = _T("Microsoft");
rs.SetFieldValue(_T("Customer_Name"), varFieldValue);
rs.Update();
This code causes only the first letters of each string to be copied to the
database with a non-UNICODE build. In this case, the letter M will be
copied to both fields.
GUID guidEngine;
#ifdef _UNICODE
guidEngine = IID_IDAODBEngineW;
#else
guidEngine = IID_IDAODBEngine;
#endif
The ANSI DAO objects require ANSI BSTRs for any strings that they receive.
When you initialize a COleVariant object using:
COleVariant varFieldValue(_T("some string"));
-or-
COleVariant varFieldValue;
varFieldValue = _T("MSFT");
The COleVariant will contain a UNICODE BSTR regardless of whether the build
is UNICODE or non-UNICODE. This is not what DAO expects.
COleVariant varFieldValue(_T("SomeString"), VT_BSTRT);
The VT_BSTRT constant specifies that the BSTR will be a UNICODE BSTR with a
UNICODE build and it will be a ANSI BSTR in a non-UNICODE build.
void FillVariant(COleVariant & var, LPCTSTR lpszSrc)
{
var.vt=VT_BSTR;
if (lpszSrc==NULL)
var.bstrVal=NULL;
else
{
#ifndef _UNICODE
int nLen = lstrlen(lpszSrc);
var.bstrVal = SysAllocStringByteLen(lpszSrc, nLen);
#else
var.bstrVal = SysAllocString(lpszSrc);
#endif
}
}
With Visual C++ 4.2 or higher, COleVariant contains a SetString() function
that can be used instead of the FillVariant() function above.
Additional query words: Seek SetFieldValue SetCacheStart SetBookMark SetParamValue
Keywords: kbdatabase kbprogramming kbprb KB140599