Link to home
Start Free TrialLog in
Avatar of Dev2003
Dev2003

asked on

Retrieving field values from a CDaoRecordset in string format

Hi,

I am trying to retrieve the values from a CDaoRecordset in string format regardless of what format it is in the Access database.

Can anyway suggest a way to do this.

I haven't had much luck using COleVariant's.

here is some code i have so far: --

//open database specified by user
CString MyMsAccessFileName;
CDaoDatabase m_DbConnection;
m_DB_Filename.GetWindowText(MyMsAccessFileName);
m_DbConnection.Open(MyMsAccessFileName);
      
CDaoTableDef tabDef(&m_DbConnection);
CDaoTableDefInfo tabInfo;
DWORD dwInfoOptions = AFX_DAO_ALL_INFO;
int nTableCount = m_DbConnection.GetTableDefCount();

for (int i=0; i < 1; i++)      {

      m_DbConnection.GetTableDefInfo(i,tabInfo);
      CString tabName = tabInfo.m_strName;
      CDaoRecordset recordset(&m_DbConnection);
      tabDef.Open(tabName);
      CString strSelect = (_T("Select * from [") + tabName + _T("];"));
      recordset.Open(dbOpenDynaset,strSelect);

      int nRecordCount = recordset.GetRecordCount();
      int nFieldCount = tabDef.GetFieldCount();

      COleVariant oleVar;

      // for each record in the table
      for (int j=0; j < nRecordCount; j++) {

            for (int k=0; k < nFieldCount; k++) {

                  CString strData;
                  recordset.GetFieldValue(k, oleVar);
                  strData = (LPCSTR)oleVar.pbstrVal;            

                  recordset.MoveNext();
        AfxMessageBox(strData);
            } //end for
      } //end for

      tabDef.Close();

} //end for

m_DbConnection.Close();
ASKER CERTIFIED SOLUTION
Avatar of mactep13
mactep13

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mactep13
mactep13

ALso, be carefult when dealing with dates.
Use COleDateTime, not CTime.

COleDateTime date = recordset.GetFiledValue("DateFiled_Or_Something").date;

There is a mismatch between Date object return by GetFieldValue and COleDateTime on dates prior to 1899. If you're interested, see MSDN on Date object and it will tell you what the mismatch is (it is minor but it does exist)

Hope this helps.
mactep
Avatar of Dev2003

ASKER

its ok.....i found a way that works.....

the following code uses a CStringArray called fieldTypes which is created by getting the data type of each field for a specific table. It is populated by converting the 'short' value received from 'm_nType' of a CDaoFieldInfo object. Also note that this code is used in a for loop using int k to go through and reference each field in a table.



recordset.GetFieldValue(k, oleVar);
CString strData;

CString strFieldType = fieldTypes.GetAt(k);
if (strFieldType.Compare("Memo") == 0) {                  strData = (LPCSTR)oleVar.pbstrVal;
} else {
                oleVar.ChangeType( VT_BSTR );
      strData = oleVar.bstrVal;
}
What you have is perfectly fine. Assuming the code that populates the CStringArray is correct, then checking agains "Memo" or "Text" or "LONG" or so on should work. That is essentially what I tried to tell you. I just included additional info if you wished to get something more than just a string.