Fog Creek Software
Discussion Board




Access / ADO / C++ Question


I've got a MEMO field in Access.

I'm trying  to get at it from ADO/C++.

The code i'm using looks something like this:

//Begin Code -----
//----------------------------
MSADO::_RecordsetPtr    pProperties;
if (!GetPropertiesRecordSet(pProperties))
return FALSE;

//Make the RecordSet a String
_variant_t vtValue = pProperties->Fields->Item[ProjectData]->Value;

CString strTemp = vtValue.bstrVal;
//End Code----
//-----------------------

The problem is that the bstr is being truncated at 255 characters.  Any idea of how I could get data that is not truncated? (MEMO fields can be LONG ...)


thanks,

Matt H.
Friday, February 14, 2003

HOWTO: Read and Write BLOBs Using GetChunk and AppendChunk

http://support.microsoft.com/default.aspx?scid=kb;en-us;194975

Rick
Friday, February 14, 2003

Caveat: I don't use BSTRings or ADO.

Are you sure the BSTR is being truncated? The length of a BSTR looks like it should be ok up to the max value of an int (why an int? can there really be negative length strings? oh well...). Are you sure that the length int is correct? That your data does not merely contain NULL characters?


Friday, February 14, 2003


it's --always-- cut off at 255.

The example is great, but it's in VB.  Any ideas how to C++-ize it?

Matt H.
Friday, February 14, 2003

Here is a link to a VC++ (ADO 2.7) sample that uses AppendChunk and GetChunk.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdmthappendchunkxvc.asp

One thing that I find useful is making sure that my memo field is the last one defined in the table and also making certain that the fields are correctly bound to your class.

Dave B.
Friday, February 14, 2003

One other thing that might help, is if you don't use a _variant_t. 

Explicity use a _bstr_t.

_bstr_t  s = pRecordSet->Fields->GetItem("Item")->Value;
printf("%s\n", (LPCSTR) s);
   

Dave B.
Friday, February 14, 2003

Everything you are doing is good. The string data in UNICODE format (for English it is ASCII value byte followed by 0 for each character representation). If you are only using it to display, you could just cast the value to LPWSTR. If you are storing it, you could use WideCharToMultiByte function.
If you do not mind to include <Atlbase.h> file in your project you could do the following-

#include <atlbase.h>
...
USES_CONVERSION;
CString sData = W2T(vData.bstrVal);

to perform the UNICODE to ANSI conversion and storing the data in class member. USES_CONVERSION and W2T macros internally uses _alloca function to dynamically allocate memory on stack to perform the conversion.

Please also see 'Item Property Example (VC++) in MSDN ADO sample
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdaexamples_vc03_5.asp

These samples use binding of ADO map fields of Recordset object C/C++ variables. These variables can be stored inside CADORecordBinding derived class for better encapsulation of your data (I think declaration of such classs although a little tedious pays off making your code look cleaner and modular).
This way you will not have to use pRS->Fields->GetItem(szCOLUMNNAME)->GetValue().bstrVal and such lengthy calls to manipulate VARIANT data.

I was able to display 750 character long Memo data with no problem. Hope you do not have NULL character at 256th location. You can observe the value at the address of the vData.bstrVal in Visual studio's Memory window to confirm.

Hope this solves your problem. Good luck.

Anup Pant
Wednesday, February 19, 2003

Sorry for all the misplaced and omitted words in previous posting.

Anup Pant
Wednesday, February 19, 2003

*  Recent Topics

*  Fog Creek Home