• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 299
  • Last Modified:

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("");

      pRS->Open(dbOpenDynaset, strQuery);

      int iRecords = pRS->GetRecordCount();

      if (!iRecords)
            pRS->SetFieldValue(2, pszArtistName);
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?
1 Solution
pRS->Open(dbOpenDynaset, strQuery);

// Instead of a record count, try...

if( rs->IsEOF( ) && rs->IsBOF( ))
 add them...

// Second, instead of adding them by ordinal...

pRS->SetFieldValue( _T( "Artist Name" ), pszArtistName);

// Also, what is your structure, if you have an auto field, then you must set it as well...

Hope this helps...


tdietzAuthor Commented:
That was the answer.  Thanks.

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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now