Solved

Using AddNew with CDaoRecordset

Posted on 1998-11-26
1
551 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
1 Comment
 
LVL 3

Accepted Solution

by:
GGRUNDY earned 50 total points
ID: 1325353
The dynaset you created with your open statement only has one
column ( ArtistID), trying to update the second column therefor
gives the grief.

Try
strQuery.Format("select * from Artists where ArtistName = '%s'", pszArtistName);

0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to determine file rename from EventLog ? 2 49
string initialization in java 11 113
Doc'in system (example?) BA 7 110
wordappend challenge 8 177
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

773 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