Using CRecordset::AddNew() and CRecordset::Edit() with IDENTITY fields 
Author Message
 Using CRecordset::AddNew() and CRecordset::Edit() with IDENTITY fields

Hey all,

I've noticed some strange behavior in the CRecordset-derived objects which
are based on tables with IDENTITY (or Autonumber) fields.  The
documentation for the CRecordset says that you should use AddNew()/Update()
and Edit()/Update() function calls to insert new records and edit existing
records in a recordset.  The problem is that when doing this on a table
which has a IDENTITY field, an exception is caused which states that you
are trying to modify an IDENTITY field, or something like that.  

How can the CRecordset functions AddNew(), Edit(), and Update(), work at
all in databases where IDENTITY fields are quite common in table
definitions (as is the case for me)?  As it is, I've resorted to using the
ExecuteSQL() member function of the CDatabase object which was used to
construct the CRecordset-derived object.  It seems, however, that the
CRecordset functions mentioned above should be sensitive to IDENTITY
fields, and not make them part of an INSERT or UPDATE statement.

Does anyone know if there is a proper way to deal with IDENTITY fields
using the CRecordset functions mentioned?  (Excluding them from the bound
fields for the CRecordset-derived class is *NOT* an option as they are used
as indices into other tables, so knowing the value of the identity field is
required.)

I've seen mention of 'parameter' fields in the DoFieldExchange
documentation, but class wizard sure doesn't bother making identity fields
into parameter members of the recordset.  Is using the parameter fields the
answer to this problem?  If so, is this a manual change that needs to be
made to the recordset class after class wizard's done with it, or can class
wizard be made to recognize identity fields and make them parameter fields
in the class?
--
Loren James Erickson

http://www.*-*-*.com/



Tue, 29 Jun 1999 03:00:00 GMT  
 Using CRecordset::AddNew() and CRecordset::Edit() with IDENTITY fields

I'm doing updates of this type using CRecordset on top of both Access and
SQLServer, with no problems.  I have the Counter/IDENTITY column bound, but
am careful not to modify the variable it is bound to.  Perhaps you're
inadvertently modifying the value of that variable?

BTW, I also need to use the IDENTITY value as a foreign key in other
tables, and hoped you might help me with a problem.  After adding a new
record, I can't determine the new counter value, because that counter is
the only unique key for the table.  In SQLServer, using snapshot
recordsets, I'm stuck trying to figure out the new id.

Good luck.



Quote:
> The problem is that when doing this on a table
> which has a IDENTITY field, an exception is caused which states that you
> are trying to modify an IDENTITY field, or something like that.  

> Does anyone know if there is a proper way to deal with IDENTITY fields
> using the CRecordset functions mentioned?  (Excluding them from the bound
> fields for the CRecordset-derived class is *NOT* an option as they are
used
> as indices into other tables, so knowing the value of the identity field
is
> required.)



Mon, 05 Jul 1999 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. CRecordSet using Edit , calling Update, Field matching

2. Slow Record Addition using VC++ CRecordSet's AddNew and Update functions

3. A Problem in Using LongBinary fields using MFC CRecordset Class

4. Slow Record Addition using CRecordSet's AddNew and Update functions

5. Accesing Identity field after AddNew()

6. Accesing Identity field after AddNew()

7. Field data truncated using a sorted CRecordset

8. CRecordset problem using single quote in field

9. Accessing Memo type fields using CRecordset

10. How to get a specific field in a table using CRecordSet

11. Using a date field in CRecordset

12. Memory access violation using CRecordset and date/time fields

 

 
Powered by phpBB® Forum Software