• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • 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("");

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?
0
tdietz
Asked:
tdietz
1 Solution
 
psdavisCommented:
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...

Phillip

0
 
tdietzAuthor Commented:
That was the answer.  Thanks.

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

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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