CString strSQL = _T("");
rAryStrMemo.RemoveAll(); // Type CStringArray
strSQL.Format(_T("SELECT memo1, memo2, memo3, memo4, memo5 FROM [Congregation Speakers] WHERE Congregation='%s' AND Speaker='%s'"),
theApp.FormatDBText(strCongregation),
theApp.FormatDBText(strSpeaker));
CRecordset *pRecords = new CRecordset(theApp.GetDatabase());
if (pRecords->Open(CRecordset::snapshot, (LPCTSTR)strSQL, CRecordset::readOnly))
{
CString strMemo = _T("");
GetFieldValue(pRecords, _T("memo1"), strMemo); rAryStrMemo.Add(strMemo);
GetFieldValue(pRecords, _T("memo2"), strMemo); rAryStrMemo.Add(strMemo);
GetFieldValue(pRecords, _T("memo3"), strMemo); rAryStrMemo.Add(strMemo);
GetFieldValue(pRecords, _T("memo4"), strMemo); rAryStrMemo.Add(strMemo);
GetFieldValue(pRecords, _T("memo5"), strMemo); rAryStrMemo.Add(strMemo);
}
}
The function it calls:
inline void GetFieldValue(CRecordset *pRS, LPCTSTR lpszFieldName, CString &rstrValue)
{
rstrValue = _T(""); // Fallback.
if (pRS != nullptr)
{
CString strTemp ;
pRS->GetFieldValue(lpszFieldName, strTemp); // This will be not right.
rstrValue = (LPCTSTR)strTemp ; // Forcing this conversion corrects it.
}
}
I learnt something new today. If the resulting CRecordset
is empty it causes an "Invalid cursor state" exception. I mistakenly thought that if Open
returned true that it had one or more records. Obviously not.
So, what is the right way to know that atleast 1 record was returned?