CDaoRecordset - adding new records 
Author Message
 CDaoRecordset - adding new records

Hi group.
In the past, you have provided invaluable help, so I would like to ask for your
guidance/knowledge again.

I am trying to add records to an open table using DAO - CDaoRecordset.  All my
code for my DAO program appears to work, except for when I try to add new records.

Here is what I am doing.  I get a .mdb file, open it, and then look through all
the tables it contains.  I then pick one of the tables, open it, and look through
all the fields it contains.  So far so good.

I write all this info into a couple of list box controls.  At the last point
where I update the list box containing the fields of the opened table, I try to
add records to the open table.  I have derived a class from CDaoRecordset and
defined a pointer as such:  CEntityMapSet* m_pSet;  // class CEntityMapSet from
public CDaoRecordset.

I have a problem when I try to use the CDaoRecordset::Open() member function.  
What I want to do is associate my m_pSet variable to the opened table and then
add data (add a record) using m_pSet.  m_pSet is configured/defined to work with
the table I am opening - all fields match, tables to field variables.

If you have a solution or advice, I would appreciate it.
Thanks,
Johnny

code example follows:

I'm having a similar problem with a CDaoRecordset...

In my program, I have defined a derived class from CDaoRecordset and linked it to
an existing database table.

In my program, I open up the database (ok), view all the tables (ok) and then
select a table and view its fields/columns (ok).

What I want to do is ADD NEW RECORDS to my table.

I have looked over DAOEnrol project in MSDN and read/re-read the info on
CDaoRecordset, CDaoTableDef, and countless class members and related articles
about the subject, but still am having problems.

Here is what is causing problems (when I try to use the m_pSet->Open() ), sorry
for the long code-segment.
Thanks in advance for guidance,
Johnny

Code follows:

// When user selects a new table name from the list control, report all
// the fields associated with the table.
void CDBEView::OnSelchangeTableList()
{
        CDaoDatabase db;
        CDaoFieldInfo fieldInfo;   // This is a structure, not a class.

        int nFields;
        CString name;

        // Get the pathname of the selected database and open that database.
        m_DBName.GetWindowText(name);
        db.Open(name);

        // Create a table object under the db database.
        CDaoTableDef table(&db);

        // Get the name of the selected table and open that table.
        m_TableList.GetText(m_TableList.GetCurSel(), name);
        table.Open(name);

        m_FieldList.ResetContent();   // This is a list box control.
        m_FieldList.SetTabStops(110);

        nFields = table.GetFieldCount();
        for (int i=0; i<nFields; i++)
        {
                table.GetFieldInfo(i, fieldInfo);
                m_FieldList.AddString(fieldInfo.m_strName);
        }
        m_FieldList.SetCurSel(0);

        MessageBox ("All is Good up to Here!");
        if (table.CanUpdate())
                MessageBox ("...and the table is open and updateable");
/***********************************************************/
        // Need to associate the CDaoRecordset object (m_pSet) to the table.
        // m_pSet is a derived object of CDaoRecordset.

        m_pSet->Open(&table,dbOpenTable,dbAppendOnly);  // This is where crashes!
        if (m_pSet->CanAppend())
        {
                m_pSet->AddNew();

                // This is a test of adding to the table.
                m_pSet->m_BOI = 500;
                m_pSet->m_entity_name = "Body of Interest";

                m_pSet->Update();
                m_pSet->Close();
        }
/***********************************************************/
        table.Close();
        db.Close();
        MessageBox ("Where did it go?");

Quote:
}



Sun, 04 May 2003 03:00:00 GMT  
 CDaoRecordset - adding new records

It looks like my code was corrupted when cut/pasting to this post.  Will send
code if needed...

Thanks again.
Johnny

Quote:
-----Original Message-----



Sun, 04 May 2003 03:00:00 GMT  
 CDaoRecordset - adding new records
johnny, I don't have an answer for you - I'm running into something
similar but simpler. I'm trying to update or add records to a table on
a SQL Server (7.0). The update is straightforward, and works fine. The
Add is refusing to cooperate. The code is virtually identical in both.
Any suggestions welcome...

The table contains 3 columns:
Row_ID: Identity (including this in the SELECT blows Open())
BarCode: numeric identifier
Structure: BLOB

Here's the UPDATE (works fine):
BOOL UpdateStruc(CDaoDatabase *pdb, long lBarCode, VARIANT *pv)
{
        ASSERT(NULL != pdb);
        ASSERT(NULL != pv);
        BOOL bResult = FALSE;
        CString sSQL;
        CDaoRecordset   rset(pdb);

        sSQL.Format("SELECT Structure, BarCode FROM StrucRawSamp WHERE
BarCode = %ld", lBarCode);
        try
        {
                rset.Open(dbOpenDynaset, sSQL, 0 );
                if (rset.IsOpen())
                {
                        rset.MoveFirst();
                        if (!(rset.IsBOF() && rset.IsEOF()))
                        {
                                rset.Edit();
                                rset.SetFieldValue(0, pv);
                                rset.Update(); //this works fine!
                                bResult = TRUE;
                        }
                        rset.Close();
                }
        }       //end of main DATABASE try-block
        catch(CDBException e)
        {
                e.ReportError();
        }
        return bResult;

Quote:
}

////////////////////////////////////////////
Here's the ADD-NEW (throws exception):
BOOL AppendStruc(CDaoDatabase *pdb, long lBarCode, VARIANT *pv)
{
        ASSERT(NULL != pdb);
        ASSERT(NULL != pv);
        BOOL bResult = FALSE;
        CString sSQL, sWhere;
        CDaoRecordset   rset(pdb);
        VARIANT vBar;
        vBar.vt = VT_I4;
        vBar.lVal = lBarCode;

        //also tried "StrucRawSamp" for SQL: doesn't work either
        sSQL.Format("SELECT Structure, BarCode FROM StrucRawSamp" );
        try
        {
                rset.Open(dbOpenDynaset, sSQL, dbAppendOnly);
                if (rset.IsOpen() && rset.CanAppend() && rset.CanUpdate
() )
                {
                                rset.AddNew(); //THROWS EXCEPTION
                                rset.SetFieldValue(0, pv);
                                rset.SetFieldValue(1, &vBar);
                                rset.Update();
                        bResult = TRUE;
                }
                rset.Close();
        }       //end of main DATABASE try-block
        catch(CDBException e)
        {
                e.ReportError();
        }
        return bResult;

Quote:
}

////////////////////////////

Sent via Deja.com http://www.deja.com/
Before you buy.



Mon, 05 May 2003 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. How to use CDaoRecordset::AddNew to create new records

2. Updating/Adding a new record that already exists in the db

3. How to Add new records!!!

4. Error adding record to new DAO database

5. Adding new record with DAO

6. CRecordset AddNew() - moving to new record added.

7. How to add new record to database?

8. How to add new record in database (DAO)?

9. Notification of new record added to table by another user

10. Adding new record - want to see defaults.

11. Add new record using OLE DB

12. Help for newbie to ADO - Best way to add a new record

 

 
Powered by phpBB® Forum Software