[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 274
  • Last Modified:

retrieving CString from Access database

Hi,

i am having trouble returning a string value from a database.
I need to return every value from a table as a string

currently i have written some code as follows....

// convert the field's value to string
CString strFieldType = fieldTypes.GetAt(k);

if (strFieldType.Compare("Memo") == 0) {
    strData = (LPCSTR)oleVar.pbstrVal;
} else {
    oleVar.ChangeType( VT_BSTR );
    strData = oleVar.bstrVal;
}


This works fine for all data types that the table would contain, however if the data type is Long and the field is empty then a 'Type mismatch' error is thrown.

does anyone have a previous method written that can do this?

am i making a meal out of this problem? is there an easier way to do this?
0
Dev2003
Asked:
Dev2003
  • 3
  • 2
1 Solution
 
nonubikCommented:
Try to check whether oleVar.vt == VT_EMPTY and make no ChangeType in this case.
0
 
Dev2003Author Commented:
i tried the following code ut i still get the type mismatch...


if (oleVar.vt == VT_EMPTY) {

    strData = "";

} else {
    if (strFieldType.Compare("Memo") == 0) {
        strData = (LPCSTR)oleVar.pbstrVal;
    } else {
        oleVar.ChangeType( VT_BSTR );
        strData = oleVar.bstrVal;
    }
}

if i miss out the change type line then it wont convert Long integers even when there is a value
0
 
Dev2003Author Commented:
ok i got the code working with the folowing....

if (strFieldType.Compare("Memo") == 0) {
    strData = (LPCSTR)oleVar.pbstrVal;      
}
                              
if (strFieldType.Compare("Long") == 0) {
            
    if (!oleVar.vt == VT_EMPTY) {
        strData = oleVar.lVal;
    } else {
        strData = "";
    }
} else {        
    oleVar.ChangeType( VT_BSTR );
    strData = oleVar.bstrVal;
}



HOWEVER....it works fine if there is no value in the Long integer field but when there is a value it doesnt convert it properly.

i know the line ...   strData = oleVar.lVal;     is not converting it properly....how can i convert the long integer value of a Variant to string properly?
0
 
nonubikCommented:
>strData = oleVar.lVal;
repalce
>strData.Format("%ld", oleVar.lVal);
0
 
Dev2003Author Commented:
cheers works superb!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now