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. Database & SysAdmin
  3. Database
  4. Help with recordsets: How to get the value of a primary key?

Help with recordsets: How to get the value of a primary key?

Scheduled Pinned Locked Moved Database
csharphelpdatabasevisual-studiotutorial
5 Posts 4 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.
  • N Offline
    N Offline
    nde_plume
    wrote on last edited by
    #1

    I have been struggling with several things in Visual Studio .Net when I upgraded from version 6.0 One thing I have had lots of problems with is the elimination of support for DaoRecordsets. I have an Access database that I want to access, and I am struggling to do it through ODBC. However, one problem I have had that I cannot seem to get past is trying to get the value of a primary auto number key out of a newly added record. With DAO there were a couple of ways to do it: rs.SetBookmark(rs.GetLastModifiedBookmark()); updates the appropriate field in the recordset. Or alternatively, using GetFieldValue which forced the field to be read from the database. CRecordset does not have a GetLastModifiedBookmark function, and when I call GetFieldValue it throws an exception. I've tried various other strategies, but can't seem to find anything that works. Any help would be much appreciated.

    N Richard DeemingR 2 Replies Last reply
    0
    • N nde_plume

      I have been struggling with several things in Visual Studio .Net when I upgraded from version 6.0 One thing I have had lots of problems with is the elimination of support for DaoRecordsets. I have an Access database that I want to access, and I am struggling to do it through ODBC. However, one problem I have had that I cannot seem to get past is trying to get the value of a primary auto number key out of a newly added record. With DAO there were a couple of ways to do it: rs.SetBookmark(rs.GetLastModifiedBookmark()); updates the appropriate field in the recordset. Or alternatively, using GetFieldValue which forced the field to be read from the database. CRecordset does not have a GetLastModifiedBookmark function, and when I call GetFieldValue it throws an exception. I've tried various other strategies, but can't seem to find anything that works. Any help would be much appreciated.

      N Offline
      N Offline
      Nick Parker
      wrote on last edited by
      #2

      nde_plume wrote: However, one problem I have had that I cannot seem to get past is trying to get the value of a primary auto number key out of a newly added record. There are several methods to do this, however here is just one that you can use in Access. Right after the insert you can run this:

      SELECT max([Auto_Number_Field])FROM [YourTable];
      

      You might also consider taking the auto number off and finding the maximum value in that column before you do the insert and then insert the maximum value + 1 for your field. HTH Nick Parker
      The greatest lesson in life is to know that even fools are right sometimes. - Winston Churchill


      D 1 Reply Last reply
      0
      • N Nick Parker

        nde_plume wrote: However, one problem I have had that I cannot seem to get past is trying to get the value of a primary auto number key out of a newly added record. There are several methods to do this, however here is just one that you can use in Access. Right after the insert you can run this:

        SELECT max([Auto_Number_Field])FROM [YourTable];
        

        You might also consider taking the auto number off and finding the maximum value in that column before you do the insert and then insert the maximum value + 1 for your field. HTH Nick Parker
        The greatest lesson in life is to know that even fools are right sometimes. - Winston Churchill


        D Offline
        D Offline
        Daniel Turini
        wrote on last edited by
        #3

        This is dangerous! Use it only inside a transaction. If another user inserts a register between your insert and your select, Ka-boom! My latest articles: Desktop Bob - Instant CP notifications XOR tricks for RAID data protection

        N 1 Reply Last reply
        0
        • D Daniel Turini

          This is dangerous! Use it only inside a transaction. If another user inserts a register between your insert and your select, Ka-boom! My latest articles: Desktop Bob - Instant CP notifications XOR tricks for RAID data protection

          N Offline
          N Offline
          Nick Parker
          wrote on last edited by
          #4

          Daniel Turini wrote: This is dangerous! Use it only inside a transaction. Very true, just another option. Transactions are a good thing, however I didn't know the exact scope of the application where this idea may work fine. Thanks for the reminder. :) Nick Parker
          The greatest lesson in life is to know that even fools are right sometimes. - Winston Churchill


          1 Reply Last reply
          0
          • N nde_plume

            I have been struggling with several things in Visual Studio .Net when I upgraded from version 6.0 One thing I have had lots of problems with is the elimination of support for DaoRecordsets. I have an Access database that I want to access, and I am struggling to do it through ODBC. However, one problem I have had that I cannot seem to get past is trying to get the value of a primary auto number key out of a newly added record. With DAO there were a couple of ways to do it: rs.SetBookmark(rs.GetLastModifiedBookmark()); updates the appropriate field in the recordset. Or alternatively, using GetFieldValue which forced the field to be read from the database. CRecordset does not have a GetLastModifiedBookmark function, and when I call GetFieldValue it throws an exception. I've tried various other strategies, but can't seem to find anything that works. Any help would be much appreciated.

            Richard DeemingR Online
            Richard DeemingR Online
            Richard Deeming
            wrote on last edited by
            #5

            If you're using Jet 4 / Access 2000, you can use SELECT @@IDENTITY to return the value.

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            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