Page 2 of 2 FirstFirst 12
Results 16 to 20 of 20
  1. #16
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi June7 (et al)

    I just tried that (Access 2010) , and encountered what I would call a nasty bug.

    I created a little 2-field test table, with autonumber a key, and added a few records (7)

    Then I did this in the immediate window:



    currentdb.Execute "Delete from table1 where key = 2"

    No problem.

    Then : currentdb.Execute "Insert into table1 values (2,""again"")" to put the record back.

    No apparent problem - the table looks OK.

    Then : currentdb.Execute "Insert into table1 (field1) values (""new record"")", dbfailonerror

    Should give new a new record with the next autonumber, right?
    But I get error 3022 - duplicate value in index or primary key.

    The re-insert of the record with key=2 reset the autonumber counter, so the next insert failed! (It actually tries to use key=3)
    This behaviour can be seen too if I try to add a new record directly in table view - the error is obvious

    Not good - I would call it a bug.

    So, I would say you cannot specify an autonumber on insert, or you may encounter serious problems later.

    John

  2. #17
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Data -

    If you are talking about using the random number feature to generate the key, I don't think you could trust that, either.

    There is always the chance that after you deleted a record, the random number used as its key would come up again, and be used for another (new) record. An attempt to restore the first one would fail.

    I think you will have to keep track of the key yourself, but it's not difficult. Each time you add a new record, make the key the current maximum plus one: New_Key = dmax("key", "yourtablename") + 1

    That way deletions and re-insertions will cause no difficulties.
    HTH

    John

  3. #18
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Interesting test, JohnG. Does appear to be bug because when doing manual data entry to the table after delete and reinsert by VBA, the next ID is 8. Then after that record is committed, the code will pick up with 9.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #19
    data808 is offline Noob
    Windows 8 Access 2007
    Join Date
    Aug 2012
    Posts
    727
    I found that the PK will start from the last number that was added to the table. So whatever PK was last entered will be what sequence it will resume. This goes for appending a record through queries as well. So as long as I append the record with the last PK to the table it will pick up from that number. I just wish there was an easier way I could control what PK I wanted to start with.

  5. #20
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Remember how ans autonumber type is designed to work - the key part is auto. Let MS Access assign the number. If you explicity assign it yourself, it does not give an error, but if you ever have to delete and then re-insert (with VBA) a record from the middle of the sequence, chances are you will get an error at some point.

    If as you said you want to control the PK values yourself, then don't use autonumber. Use Integer or Long, set the field to required, and put a unique index on it. You can use the DMax funtion to determine the current maximum value:

    dMax("PK","Tablename")

    John

Page 2 of 2 FirstFirst 12
Please reply to this thread with any new information or opinions.

Similar Threads

  1. AutoNumber Primary Key until manually entered
    By anonymust in forum Database Design
    Replies: 3
    Last Post: 05-11-2014, 08:34 AM
  2. Using AutoNumber primary key on sorted column
    By grkatz823 in forum Access
    Replies: 7
    Last Post: 03-04-2013, 03:47 PM
  3. Sequential autonumber primary key
    By JackCampion in forum Access
    Replies: 0
    Last Post: 09-17-2012, 05:07 PM
  4. Replies: 4
    Last Post: 05-07-2012, 12:08 PM
  5. Replies: 2
    Last Post: 04-30-2010, 09:43 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