?
Solved

writing data to Access database

Posted on 2005-04-19
2
Medium Priority
?
262 Views
Last Modified: 2010-04-24
Hi,
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;
        AfxMessageBox(pStr);
    }

    recordset.SetFieldValue(nCurrentField, oleVar);

}


thank you
0
Comment
Question by:Dev2003
2 Comments
 
LVL 16

Accepted Solution

by:
nonubik earned 1000 total points
ID: 13814379
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;
        //AfxMessageBox(pStr);
        recordset.SetFieldValue(nCurrentField, fieldData);  //assuming fieldData is a string
    }

   
0
 

Author Comment

by:Dev2003
ID: 13820292
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
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

612 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