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.GetWindowTex t(MyMsAcce ssFileName );
m_DbConnection.Open(MyMsAc cessFileNa me);
CDaoTableDef tabDef(&m_DbConnection);
CDaoTableDefInfo tabInfo;
DWORD dwInfoOptions = AFX_DAO_ALL_INFO;
int nTableCount = m_DbConnection.GetTableDef Count();
for (int i=0; i < 1; i++) {
m_DbConnection.GetTableDef Info(i,tab Info);
CString tabName = tabInfo.m_strName;
CDaoRecordset recordset(&m_DbConnection) ;
tabDef.Open(tabName);
CString strSelect = (_T("Select * from [") + tabName + _T("];"));
recordset.Open(dbOpenDynas et,strSele ct);
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();
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.GetWindowTex
m_DbConnection.Open(MyMsAc
CDaoTableDef tabDef(&m_DbConnection);
CDaoTableDefInfo tabInfo;
DWORD dwInfoOptions = AFX_DAO_ALL_INFO;
int nTableCount = m_DbConnection.GetTableDef
for (int i=0; i < 1; i++) {
m_DbConnection.GetTableDef
CString tabName = tabInfo.m_strName;
CDaoRecordset recordset(&m_DbConnection)
tabDef.Open(tabName);
CString strSelect = (_T("Select * from [") + tabName + _T("];"));
recordset.Open(dbOpenDynas
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,
strData = (LPCSTR)oleVar.pbstrVal;
recordset.MoveNext();
AfxMessageBox(strData);
} //end for
} //end for
tabDef.Close();
} //end for
m_DbConnection.Close();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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("Mem o") == 0) { strData = (LPCSTR)oleVar.pbstrVal;
} else {
oleVar.ChangeType( VT_BSTR );
strData = oleVar.bstrVal;
}
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,
CString strData;
CString strFieldType = fieldTypes.GetAt(k);
if (strFieldType.Compare("Mem
} 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.
Use COleDateTime, not CTime.
COleDateTime date = recordset.GetFiledValue("D
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