HOWTO: Retrieve a Counter Field Value Without Requerying

Last reviewed: June 26, 1997
Article ID: Q132397
The information in this article applies to:
  • The Microsoft Foundation Classes (MFC) included with: - Microsoft Visual C++ for Windows, version 1.52 - Microsoft Visual C++, 32-bit Edition, versions 2.1, 2.2, 4.0, 5.0

SUMMARY

The Microsoft Access ODBC driver version 2.x provides a Counter field data type. This is an autoincrement field, meaning that the value of the field is assigned automatically by the ODBC driver when a record is first created. This value can then be used as a primary key or as general index.

Once a record is added, the value of the counter field is assigned by the Microsoft Access ODBC driver version 2.x. You can save this value for later use, and have an application retrieve the value of the counter field without requerying. This article explains how.

MORE INFORMATION

ODBC doesn't supply any mechanism by which an application can easily retrieve the value of an autoincrement field when a record is added. However, the Microsoft Access ODBC driver version 2.x does provide a feature that can help. The Microsoft Access ODBC driver version 2.x appends newly added records to the end of the current recordset if you use a dynaset. After adding a record, an application can call MoveLast() to move to the last record of the recordset to retrieve the value of the new record.

If you want to move back to the previous cursor position following the MoveLast() call, use the information provided in the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q132398
   TITLE     : HOWTO: Use Bookmarks with the MFC ODBC Database Classes

Sample Code

The following code segment is an example of how to use the bookmark code in article Q132398 to retrieve the value of a counter (autoincrement) field in Microsoft Access version 2.0 so that you can save the value for later use within your application. This example moves to a record, gets the bookmark for the record, adds a record, calls MoveLast() to get to the newly added record and thus retrieve the counter field value, and then moves back to the record it came from.

   CDatabase db;
   db.Open("MYDataSourceName",NULL,NULL,"ODBC;",FALSE);
   CCounterSet rs(&db);

   // Open up dynaset because any records added by a CRecordset using
   // dynasets are appended to the end of Microsoft Access 2.0 recordset

   rs.Open(CRecordset::dynaset);

   // move to some record
   rs.MoveNext();

   BOOKMARK bookmark;
   rs.GetBookmark(&bookmark);

   // Add record. Counter field of record will be given a value by
   // the ODBC driver
   rs.AddNew();
   rs.m_textfield="SomeText";
   rs.Update();

   // MoveLast to get on record just added
   rs.MoveLast();

   // retrieve the value of the counter field for the
   // newly added record here

   // return to the record you were on
   rs.MoveToBookmark(bookmark);

REFERENCES

For more information, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q124915
   TITLE     : SAMPLE: Using Dynasets with the 16-bit MFC Database Classes


Additional reference words: 1.50 1.51
Keywords : kbcode kbprg MfcDatabase
Technology : kbMfc
Version : 1.52 2.1 2.2 4.0 5.0
Platform : NT WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 26, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.