Helpful MFC Classes Used in DAO

DAO makes extensive use the COleVariant, CString, and COleDate Time Microsoft Foundation Classes (MFC). Objects from these classes are passed to and from many DAO methods and properties. These classes also provide helpful methods for data comparison and manipulation that make developing applications easier. Some methods of the COleVariant and CString classes are listed below.

When debugging applications involving COleVariants, it's helpful to have a way to convert the variant to a displayable string. One way to do this is to use the CString::Format method. For example:

   COleVariant   vVal(1000, VT_I4);
   CString      strVal;
   //
   strVal.Format("Value = %ld\n", vVal.lVal);

Another way is to use the COleVariant::ChangeType method to convert the variant to a BSTR string. The variant itself can be converted, or another variant can be used as the source of the data. The string that results is always Unicode, unless the variant was created with the VT_BSTRT variant type. For example:

   COleVariant   vVal(1000, VT_I4),
               vUni(_T("unicode")),
               vAnsi(_T("ansi"), VT_BSTRT);
   //
   vVal.ChangeType(VT_BSTR);    // Convert to L"1000".
   vUni.ChangeCode(VT_BSTR);  // No change.
   vAnsi.ChangeType(VT_BSTR); // Also, no change.

Note By default, the COleVariant string constructor creates a variant with a Unicode value and a VT_BSTR variant type. In non-Unicode builds, DAO methods like SetField, Seek, etc., require variants with an ANSI string value. ANSI strings are constructed when you specify the optional variant type, VT_BSTRT. However, the resulting variant is still marked with the VT_BSTR variant type.

The COleVariant::ChangeType method will not convert a BSTR variant to Unicode, even if its value is really an ANSI string. When displaying the string you should be careful to use appropriate Unicode or ANSI formatting, or a function like wcstombs, which converts a Unicode string to multi-character.

Another debugging tool for formatting variants is the CCrack class. This is not a standard MFC class, but a part of the DAOVIEW sample distributed with Microsoft Visual C++. It uses the CString class to either format the value of a variant as a displayable string, or create a string describing the variant type. You can adapt this class to your purposes as needed.

COleVariant

Construction Description
COleVariant Constructs a COleVariant object from several data types, including short, long, null-terminated string, CString, Boolean, variant, date/time, double, and float.

Method Description
Attach Attaches a VARIANT to a COleVariant.
ChangeType Changes the variant type of this COleVariant object.

Operators Description
operator LPCVARIANT Converts a COleVariant value into an LPCVARIANT.
operator LPVARIANT Converts a COleVariant object into an LPVARIANT.
operator = Copies a COleVariant value.
operator == Compares two COleVariant values.

CString

Construction Description
CString Constructs CString objects from CStrings, null-terminated single-byte and wide-byte strings.

Method Description
GetLength Returns the number of characters in a CString object.
IsEmpty Tests whether a CString object contains no characters.
Empty Forces a string to have 0 length.
Compare Compares two strings (case sensitive).
CompareNoCase Compares two strings (case insensitive).
Format Format the string as sprintf does.

Operator Description
operator = Assigns a new value to a CString object.
operator + Concatenates two strings and returns a new string.
operator += Concatenates a new string to the end of an existing string.
operator == <, etc. Comparison operators (case sensitive).
operator LPCTSTR Directly accesses characters stored in a CString object as a C-style string.
operator [] Returns the character at a given position — operator substitution for GetAt.