tdietz
asked on
Using AddNew with CDaoRecordset
I am having trouble using CDaoRecordset::AddNew. Basically I am trying to scan a table for a name. If that name is not there, I want to add it. Here is my code:
strQuery.Format("select ArtistID from Artists where ArtistName = '%s'", pszArtistName);
CDaoRecordset* pRS = new CDaoRecordset(m_pDB);
CString cs("");
try
{
pRS->Open(dbOpenDynaset, strQuery);
int iRecords = pRS->GetRecordCount();
if (!iRecords)
{
pRS->AddNew();
pRS->SetFieldValue(2, pszArtistName);
pRS->Update();
}
}
catch (CDaoException* e)
{
AfxMessageBox(e->m_pErrorI nfo->m_str Descriptio n, MB_ICONEXCLAMATION);
}
----
The problem is that every time I try to call SetFieldValue I get an exception that says "Item not found in this collection". The new row (blank however) is added to the table, I just can't set my data for that row.
How can I accomplish this?
strQuery.Format("select ArtistID from Artists where ArtistName = '%s'", pszArtistName);
CDaoRecordset* pRS = new CDaoRecordset(m_pDB);
CString cs("");
try
{
pRS->Open(dbOpenDynaset, strQuery);
int iRecords = pRS->GetRecordCount();
if (!iRecords)
{
pRS->AddNew();
pRS->SetFieldValue(2, pszArtistName);
pRS->Update();
}
}
catch (CDaoException* e)
{
AfxMessageBox(e->m_pErrorI
}
----
The problem is that every time I try to call SetFieldValue I get an exception that says "Item not found in this collection". The new row (blank however) is added to the table, I just can't set my data for that row.
How can I accomplish this?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I can't believe I didn't see that right away....