Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CRecordset, ODBC, and Access

CRecordset, ODBC, and Access

Scheduled Pinned Locked Moved C / C++ / MFC
databasequestionannouncement
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    David Crow
    wrote on last edited by
    #1

    I've a table in an Access database with an AutoNumber field. When I add a row to the table through a CRecordset object, I want to know the value of the AutoNumber field. I've read various articles on how this is done, but none of them work. The code basically looks like:

    CMyRecordset set;
    CDBVariant bookmark;
    set.Open(AFX_DB_USE_DEFAULT_TYPE, NULL, CRecordset::useBookmarks);
    set.AddNew();
    set.field1 = 123;
    set.field2 = 456;
    ...
    set.GetBookmark(bookmark);
    // the value of 'bookmark' at this point is invalid since the recordset has
    not been updated

    set.Update(); // this adds the new record, but also makes the first record
    the current one

    set.Requery();
    set.SetBookmark(bookmark); // this has no effect

    I successfully use bookmarks in other areas of the program when editing rows. I populate a listbox with each record in a recordset and assign the AutoNumber field using SetItemData(). When an item in the listbox is selected and the Edit button is clicked, I have the correct bookmark and can then use SetBookmark() and Edit() successfully. Using this scenario, if a newly added listbox item does not have a correct bookmark associated with it, SetBookmark() cannot be used prior to Edit() or Delete() calls. In other words, I have no way to navigate to the desired record. Any ideas for this long-winded request? Thanks, DC


    Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

    T S 2 Replies Last reply
    0
    • D David Crow

      I've a table in an Access database with an AutoNumber field. When I add a row to the table through a CRecordset object, I want to know the value of the AutoNumber field. I've read various articles on how this is done, but none of them work. The code basically looks like:

      CMyRecordset set;
      CDBVariant bookmark;
      set.Open(AFX_DB_USE_DEFAULT_TYPE, NULL, CRecordset::useBookmarks);
      set.AddNew();
      set.field1 = 123;
      set.field2 = 456;
      ...
      set.GetBookmark(bookmark);
      // the value of 'bookmark' at this point is invalid since the recordset has
      not been updated

      set.Update(); // this adds the new record, but also makes the first record
      the current one

      set.Requery();
      set.SetBookmark(bookmark); // this has no effect

      I successfully use bookmarks in other areas of the program when editing rows. I populate a listbox with each record in a recordset and assign the AutoNumber field using SetItemData(). When an item in the listbox is selected and the Edit button is clicked, I have the correct bookmark and can then use SetBookmark() and Edit() successfully. Using this scenario, if a newly added listbox item does not have a correct bookmark associated with it, SetBookmark() cannot be used prior to Edit() or Delete() calls. In other words, I have no way to navigate to the desired record. Any ideas for this long-winded request? Thanks, DC


      Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

      T Offline
      T Offline
      Terry ONolley
      wrote on last edited by
      #2

      In theory - you need to obtain an exclusive lock to your datasource (I don't know how to do this because depending on ODBC drivers/ADO vrs DAO/etc. it changes). Then do the insert. Then query for MAX on the ID column. This will give you access to the newly inserted record. Then unlock it.


      Have you answered an MTQ? Check out the stats!
      What's the latest butt-scratch count? Check it out!

      D 1 Reply Last reply
      0
      • D David Crow

        I've a table in an Access database with an AutoNumber field. When I add a row to the table through a CRecordset object, I want to know the value of the AutoNumber field. I've read various articles on how this is done, but none of them work. The code basically looks like:

        CMyRecordset set;
        CDBVariant bookmark;
        set.Open(AFX_DB_USE_DEFAULT_TYPE, NULL, CRecordset::useBookmarks);
        set.AddNew();
        set.field1 = 123;
        set.field2 = 456;
        ...
        set.GetBookmark(bookmark);
        // the value of 'bookmark' at this point is invalid since the recordset has
        not been updated

        set.Update(); // this adds the new record, but also makes the first record
        the current one

        set.Requery();
        set.SetBookmark(bookmark); // this has no effect

        I successfully use bookmarks in other areas of the program when editing rows. I populate a listbox with each record in a recordset and assign the AutoNumber field using SetItemData(). When an item in the listbox is selected and the Edit button is clicked, I have the correct bookmark and can then use SetBookmark() and Edit() successfully. Using this scenario, if a newly added listbox item does not have a correct bookmark associated with it, SetBookmark() cannot be used prior to Edit() or Delete() calls. In other words, I have no way to navigate to the desired record. Any ideas for this long-winded request? Thanks, DC


        Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

        S Offline
        S Offline
        sam India
        wrote on last edited by
        #3

        after update you are requerying and setting the bookmark. this is wrong. you can get bookmark of a current record after you update. and bookmarks are valid only for navigating in the current recordsets. once you close and reopen or requery then the bookmarks get vanished. you should call recordset.canbookmark() to check whether it is possible to bookmark or not. use CDatabase::GetBookmarkPersistence. always use exception handling in database programming to get the actual cause of erro. never say die

        D 1 Reply Last reply
        0
        • S sam India

          after update you are requerying and setting the bookmark. this is wrong. you can get bookmark of a current record after you update. and bookmarks are valid only for navigating in the current recordsets. once you close and reopen or requery then the bookmarks get vanished. you should call recordset.canbookmark() to check whether it is possible to bookmark or not. use CDatabase::GetBookmarkPersistence. always use exception handling in database programming to get the actual cause of erro. never say die

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          sam India wrote: after update you are requerying and setting the bookmark. this is wrong. Please explain. sam India wrote: you can get bookmark of a current record after you update. Which is what I am doing. sam India wrote: and bookmarks are valid only for navigating in the current recordsets. once you close and reopen or requery then the bookmarks get vanished. you should call recordset.canbookmark() to check whether it is possible to bookmark or not. use CDatabase::GetBookmarkPersistence. As I already indicated, bookmarks are used elsewhere so CanBookmark() was omitted from the code snippet. I have used CheckBookmarkPersistence() to verify that bookmarks support the SQL_BP_SCROLL, SQL_BP_UPDATE, and SQL_BP_DELETE operations. sam India wrote: always use exception handling in database programming to get the actual cause of erro. Exception handling is in place. I omitted it from the code snippet for brevity.


          Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

          1 Reply Last reply
          0
          • T Terry ONolley

            In theory - you need to obtain an exclusive lock to your datasource (I don't know how to do this because depending on ODBC drivers/ADO vrs DAO/etc. it changes). Then do the insert. Then query for MAX on the ID column. This will give you access to the newly inserted record. Then unlock it.


            Have you answered an MTQ? Check out the stats!
            What's the latest butt-scratch count? Check it out!

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            Terry O`Nolley wrote: In theory - you need to obtain an exclusive lock to your datasource... I'll try this, but I'm not following your logic. What would an exclusive lock on a single-user database have to with being able to query an AutoNumber field of a newly-added record? I guess my main concern is that the dozen or so examples I looked at on MSDN and the Internet all showed basically the same approach: after an Update(), get the bookmark, call Requery(), set the bookmark, get the AutoNumber field value. The problem seems to be that after the call to Update(), the recordset is positioned back at the first record.


            Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

            T 1 Reply Last reply
            0
            • D David Crow

              Terry O`Nolley wrote: In theory - you need to obtain an exclusive lock to your datasource... I'll try this, but I'm not following your logic. What would an exclusive lock on a single-user database have to with being able to query an AutoNumber field of a newly-added record? I guess my main concern is that the dozen or so examples I looked at on MSDN and the Internet all showed basically the same approach: after an Update(), get the bookmark, call Requery(), set the bookmark, get the AutoNumber field value. The problem seems to be that after the call to Update(), the recordset is positioned back at the first record.


              Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

              T Offline
              T Offline
              Terry ONolley
              wrote on last edited by
              #6

              DavidCrow wrote: I'll try this, but I'm not following your logic. What would an exclusive lock on a single-user database have to with being able to query an AutoNumber field of a newly-added record? I didn't realize it was a single-user DB. If it is a single user DB then you don't need the lock. Just do the insert and query for MAX ID. If your ID field is an auto-number, then the MAX will be the newly inserted record. Using LOCK prevents other records from being inserted while you do your insert.


              Have you answered an MTQ? Check out the stats!
              What's the latest butt-scratch count? Check it out!

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups