Results 1 to 6 of 6
  1. #1
    yosteee is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Oct 2017
    Posts
    5

    changing a number field to a autonumber


    Many years ago, when I first started in DB, teaching myself, I created a Database that has a number field as the primary key. This should have actually been an auto number field. However, this field started with a -149 value and is currently up to 1027, manually entering a sequential number for each new record. We started with a negative number because after starting using the DB, it was decided to go back three years worth of work and add them to the DB.
    1. I would like this field to "automatically" increase by one for each new record. What is the best SAFE way to accomplish this?

  2. #2
    ranman256's Avatar
    ranman256 is online now VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    you cannot turn numbers into autonumbers.
    you can save the existing numbers, create a new autonum field,
    then backfill all related tables to the new autonum using update queries.

    Convert to autonum:
    rename ID (orig ID) to ID_old (in all tables)
    create ID (as autonum) in master table
    now join tables on ID_Old and fill in the ID on all tables.

  3. #3
    Join Date
    Apr 2017
    Posts
    1,679
    Make a copy of your DB!

    In case you used relationships at table level and referential integrity, remove them.

    Create a empty table with structure identical with table having numeric PK, but without PK defined. Add an autonumeric field, and define it as PK.

    Run insert query to copy data from original table into lately created (autonumeric PK field is filled automatically, don't include it into query.

    Run update queries for all tables having earlier numeric field as FK, replacing it witch according autonumeric field value from new table.

    Rename/delete old table. Rename the new table with name the old table had.

    In case you had table-level relationships/referential integrity, recreate them.

    Check, is all OK.

  4. #4
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Quote Originally Posted by yosteee View Post
    I would like this field to "automatically" increase by one for each new record. What is the best SAFE way to accomplish this?
    You might want to rethink switching to an autonumber if you need sequential numbers.
    Is this a multi-user dB? If so, you need to take that into account.


    AutoNumber
    ----------------
    Purpose: Use an AutoNumber field to provide a unique value that serves no other purpose than to make each record unique. The most common use for an AutoNumber field is as a primary key


    Autonumbers--What they are NOT and What They Are
    http://www.utteraccess.com/wiki/Autonumbers


    An autonumber type field is not guaranteed to be sequential, gap less nor is it guaranteed to be positive. It is only guaranteed to be UNIQUE.
    You need to create a "Custom Autonumber" routine. There are lot of those on the web.

    You could check out:
    Scott Gem's site https://scottgem.wordpress.com/2009/...ial-numbering/
    Paul's site: http://baldyweb.com/CustomAutonumber.htm


    Google "custom auto number in access" (no quotes)

  5. #5
    Join Date
    Apr 2017
    Posts
    1,679
    Quote Originally Posted by ssanfu View Post
    You need to create a "Custom Autonumber" routine. There are lot of those on the web.
    It looks like OP has the custom routine or autonumeric PK until now too. Simply OP wants the numbering started from 0 instead of -149.

    Btw, as addition to my previous post, before assigning new FK values to other tables, possible orphan records (there is no matching old PK value in table OP is changing for FK in another table) from those must be deleted - otherwise some of them may be attached to some record in main table, where they don't belong.

  6. #6
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    @Arvil,
    Quote Originally Posted by yosteee View Post
    <snip>However, this field started with a -149 value and is currently up to 1027, manually entering a sequential number for each new record.<snip>
    In post #1, the OP indicates they manually enter the number. So if they need a sequential number (with no gaps) AND they have multiple users, an Autonumber type field in not a good choice.
    If they have an autonumber type field for the PK field AND they keep their "number field", I support that.
    They could still create a "Custom Autonumber" routine to enter a sequential number in their "number field".

    My $0.02.......

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

Similar Threads

  1. Replies: 8
    Last Post: 12-14-2015, 07:02 AM
  2. changing field on table from Text to Number
    By fainterm in forum Queries
    Replies: 2
    Last Post: 01-20-2014, 03:34 PM
  3. Replies: 4
    Last Post: 11-21-2013, 05:06 PM
  4. Replies: 2
    Last Post: 03-19-2013, 09:36 PM
  5. Replies: 8
    Last Post: 01-30-2013, 06:40 AM

Tags for this Thread

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