Link to home
Start Free TrialLog in
Avatar of tdietz
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_pErrorInfo->m_strDescription, 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?
ASKER CERTIFIED SOLUTION
Avatar of psdavis
psdavis
Flag of United States of America image

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 tdietz
tdietz

ASKER

That was the answer.  Thanks.

I can't believe I didn't see that right away....