CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2002
    Location
    Australia
    Posts
    54

    Problem: Saving COleVariant fields in CDaoRecordset

    HI,

    I am having a problem saving, retrieving and searching on fields in my dao recordset (Access database). I am saving a number of text fields into a table in the following manner:

    Code:
    m_pRecordset->SetFieldValue(m_Fields[i].cFieldName, cElements[i]);
    Where cElements is an array of COleVariants ... each one being converted from a CString in the following way:
    Code:
    CString field1("test");
    varArray[0] = COleVariant(field1);
    The fields are all added fine, but when I open the database in Access they appear only as one character. eg: For the 'test' example, the field appears as 't' only. When I retrieve the strings from the database using the following code:
    Code:
    m_pRecordset->GetFieldValue(cFieldName, temp);
    CString field1 = (CString)temp.bstrVal;
    I get the full field value of 'test'. So - this is seems alright, although I could not work out why only the one character is seen in Access.

    When I go to search on the field however, I run into problems. I can never find the record whose key value = 'test' because it appears to be comparing it to 't' rather than the full string.

    Is this clear? Can anyone help with this issue?

    thanks,
    Em

  2. #2
    Join Date
    Apr 2002
    Location
    Australia
    Posts
    54
    Hi - Just wondering if anyone has experienced this problem before? I have not been able to resolve it yet and have tried many different options.

    I tried inserting strings into the database fields, rather than variants - this worked fine, but then I could not access the contents again!

    If anyone could help I'd be most appreciative.

    thanks,
    Em

  3. #3
    Join Date
    Apr 2003
    Posts
    1,755

    Smile

    The CRecordset treats BSTR as byte array, not as real BSTRs. To store the string correctly, use this code
    Code:
    CString field1("test");
    VARIANT variant;
    ::VariantInit(&variant);
    variant.vt = VT_BSTR;
    variant.bstrVal = ::SysAllocStringByteLen((LPCTSTR)field1, field1.GetLength());
    varArray[0] = COleVariant(variant);
    ::VariantClear(&variant);
    We converted the CString to BSTR but the BSTR should still be in ANSI form.

    To get it from the database
    Code:
    m_pRecordset->GetFieldValue(cFieldName, temp);
    CString field1 = (LPCTSTR)temp.bstrVal;
    We placed the value of temp.bstrVal simply by typecasting it to LPCTSTR to avoid conversion of UNICODE to ANSI since we saved the value as an ANSI BSTR.

    Hope it will help you

  4. #4
    Join Date
    Apr 2002
    Location
    Australia
    Posts
    54

    Thumbs up

    rxbagain - thank you so much
    the code works wonderfully,

    I really appreciate your help!
    Emelia

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured