Solved

Using AddNew with CDaoRecordset

Posted on 1998-11-26
2
280 Views
Last Modified: 2013-11-20
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
Comment
Question by:tdietz
2 Comments
 
LVL 7

Accepted Solution

by:
psdavis earned 50 total points
ID: 1325354
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
 

Author Comment

by:tdietz
ID: 1325355
That was the answer.  Thanks.

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

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Tab names are off by one 5 49
if loop in java 3 114
Smart Camera scanning and reading information 3 86
MaxSpan challenge 9 67
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now