Click to See Complete Forum and Search --> : Autonumbers and CRecordSet::Requery()


DanielF
September 28th, 1999, 09:39 AM
Hi All,

I'm inserting a new record into a database using CRecordSet::AddNew, followed by CRecordSet::Update, However one of the key fields is an auto-number, I call the requery method, but it returns the first record. How do I get it to return the record I've just inserted?

Many Thanks

Daniel.

Oleg Lobach
September 28th, 1999, 09:50 AM
Hi,
Use CRecordset::m_strSort,like this:

rst.AddNew(..);
rst.Update(..);
rst.m_strSort="Your-auto-number-key-field DESC"
rst.Requery()




Hope this helps,
Oleg.

cherivsk
September 28th, 1999, 09:51 AM
Requery using auto-number field as desc order, than get the field value and use it.

DanielF
September 29th, 1999, 01:46 AM
The filter method should work.....however I'm worried that should the database become busy, as many copies of the software should be running, that between the Update and Requery of one copy that another copy might call an AddNew and Update. This, I would assume, would return the record of the second application not the correct record of the first application.

Any Thoughts? Suggestions?

Thanks Daniel.

Oleg Lobach
September 29th, 1999, 07:41 AM
Hi Daniel.
You can open your recordset in the following modes:
- CRecordset::dynaset
- CRecordset::snapshot
- CRecordset::dynamic
- CRecordset::forwardOnly

A dynaset is a recordset with dynamic properties. During its lifetime, a recordset object in dynaset mode (usually called simply a “dynaset”) stays synchronized with the data source in the following way. In a multiuser environment, other users may edit or delete records that are in your dynaset or add records to the table your dynaset represents. Records your application adds to or deletes from the recordset are reflected in your dynaset. Records that other users add to the table will not be reflected in your dynaset until you rebuild the dynaset by calling its Requery member function. When other users delete records, MFC code skips over the deletions in your recordset. Other users’ editing changes to existing records are reflected in your dynaset as soon as you scroll to the affected record.

In the snapshot mode the membership and ordering of the records are determined when the recordset is opened; the data values are determined when the records are fetched. Changes made by other users are not visible until the recordset is closed and then reopened.

Dynamic recordsets (CRecordset::dynamic) are similar to dynasets; however, dynamic recordsets reflect any records added or deleted without calling CRecordset::Requery. For this reason, dynamic recordsets are generally expensive with respect to processing time on the DBMS, and many ODBC drivers do not support them.

The forwardOnly mode produce a read-only recordset with only forward scrolling.Changes made by other users are not visible until the recordset is closed and then reopened.

So, to avoid problems with unexpected changes in recordset by other applications - don't use CRecordset::dynamic mode.

Hope this helps,
Oleg.

Rares
September 29th, 1999, 09:27 AM
After you did an .Update() do a .MoveLast()
There it is!
When using CRecordsets (dynaset) and adding a new record, that record will be placed at the end of the recordset.


... But I thought YOU did the backups...