Results 1 to 8 of 8
  1. #1
    ExcessionOCP is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    May 2023
    Posts
    31

    "Autonumber" issue post-SQL migration

    Hi All,



    In what was probably a massive bodge, I had a form with a hidden field linked to to an Autonumber ID field in the source table that served as the record's ID and PK. When my users started entering information into the form, this field was automatically updated with the record's ID, which I then went on to use when the user submitted the form to perform some data manipulation before saving the record.

    The problem is, that now I have migrated the app's back end to SQL server, this field is no longer populated when the users start entering data, and I suspect isn't even generated until the record is saved and SQL server does its thing to write the record to the DB.

    Does anyone have any other suggestions for how I can access the record's ID before it is saved? Now that I type this out I'm realising how much of a bodge this must have been and that there's probably a much better, safer, faster way of doing this.

    Cheers,

    Phil

  2. #2
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    I have seen this problem with conversions from Access to SQL before, and depending on how the form is designed it can be a simple or painful fix.
    As you surmised, you only get the ID as soon as the record is saved.

    So the only real option is - force a save early doors in the process, then do your other processing.

    You can do an insert on the table in code on going to a new record, and then retrieve the new empty record as the "New" record, if they cancel then you are caught having to delete the record or marking it as "Deleted" in some way.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I have an older app where I had this situation, I grabbed the ID from a bound textbox in the after insert event of the form.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    ExcessionOCP is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    May 2023
    Posts
    31
    Quote Originally Posted by pbaldy View Post
    I have an older app where I had this situation, I grabbed the ID from a bound textbox in the after insert event of the form.
    Hi, could you give me a bit more info about the after insert event? This isn't an event I've used in the past.

  5. #5
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Quote Originally Posted by ExcessionOCP View Post
    Hi, could you give me a bit more info about the after insert event? This isn't an event I've used in the past.
    https://learn.microsoft.com/en-us/of...erinsert(even)
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    Join Date
    Apr 2017
    Posts
    1,681
    One way to correct such issues:

    In SQL server, create a table like the Access one with a new autonumber ID, + a long integer field for Access autonumber value (my advice is, name it something like MyOldId, and the new key field same as old one in Access);
    Insert all data (previous autonumeric id included) from Access table into this SQL Server table - all records get a new autonumeric ID, and in another field, you'll have values of old Access autonumeric ID's;
    In SQL Server, in all tables which contain foreign keys linked to old Access ID field, rename all those old foreign keys, and into every such table, add a field for saving new foreign key with old name. Run update queries for all such tables (set new foreign keys to matching primary key value, comparing old keys in WHERE clause);
    Remove all renamed fields which contain old ID's and foreign keys from all SQL Server tables.

  7. #7
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,044
    I suppose you let the number generate through the identity specification property of the PK field. If you want to use the number before the record is saved, you can create a sequence on SQL server and take the number with a select statement like
    Code:
    select next value  for MySeq

  8. #8
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Some more reading for you :
    https://www.red-gate.com/simple-talk...ntity-columns/
    Using the sequence as already suggested would probably be the safest, you can also try the IDENT_CURRENT plus IDENT_INCR if your db doesn't have a lot of concurrent users that might impact the results.
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 10-24-2022, 06:38 PM
  2. Replies: 8
    Last Post: 04-21-2021, 09:44 AM
  3. Replies: 4
    Last Post: 04-12-2019, 09:47 AM
  4. Replies: 1
    Last Post: 09-07-2015, 08:00 AM
  5. Replies: 1
    Last Post: 01-18-2015, 07:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums