writing data to Access database

Posted on 2005-04-19
Last Modified: 2010-04-24
i have written the following method to write a string to a MS Access database using a recordset. If the field type is Long then it converts the string to a Long integer.

However, if the field type is a string the method will only write the first character of the string to the database. I know that the commented out line does not work properly. I assume that the COleVariant constructor is misinterpreting the data type being passed.

so i tried using the setstring method but the same happens.

does anyone know how i can get it to write the whole string to the database, either by changing my code or by showing me some other code that works.

void CDBConverterDlg::set_field_value(CDaoRecordset& recordset, int nCurrentField, CString fieldData, CString fieldType) {

    COleVariant oleVar;
    if (!fieldType.Compare("Long")) {
        long number = atoi(fieldData);
        oleVar = new COleVariant(number , VT_I4);
    } else {
        CString pStr;
        //oleVar = new COleVariant(pStr);
        oleVar.SetString(fieldData, VT_BSTR);
        pStr = (LPCTSTR)oleVar.pbstrVal;

    recordset.SetFieldValue(nCurrentField, oleVar);


thank you
Question by:Dev2003
    LVL 16

    Accepted Solution

    From MSDN:
    "Note that if you are not creating a UNICODE recordset, you must either use a form of SetFieldValue that does not contain a COleVariant parameter, or the COleVariant object must be explicitly declared ANSI. This can be done by using the COleVariant::COleVariant( lpszSrc, vtSrc ) form of constructor with vtSrc set to VT_BSTRT (ANSI) or by using the COleVariant function SetString( lpszSrc, vtSrc ) with vtSrc set to VT_BSTRT."

    But easier, you can use the
      void SetFieldValue(int nIndex, LPCTSTR lpszValue);
    form of the SetFieldMethod.

     COleVariant oleVar;
        if (!fieldType.Compare("Long")) {
            long number = atoi(fieldData);
            oleVar = new COleVariant(number , VT_I4);
            recordset.SetFieldValue(nCurrentField, oleVar);
        } else {
            //CString pStr;
            //oleVar = new COleVariant(pStr);
            //oleVar.SetString(fieldData, VT_BSTR);
            //pStr = (LPCTSTR)oleVar.pbstrVal;
            recordset.SetFieldValue(nCurrentField, fieldData);  //assuming fieldData is a string


    Author Comment

    yeah thats works fine now. I used the SetFieldValue method with the string. It threw the following error with a CString but i converted it to a LPCTSTR variable first.

    \DBConverterDlg.cpp(796) : error C2668: 'SetFieldValue' : ambiguous call to overloaded function

    thanks for your help

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
    In Easy String Encryption Using CryptoAPI in C++ ( I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now