3 Replies - 783 Views - Last Post: 10 March 2010 - 04:59 AM Rate Topic: -----

#1 Ahmedn1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 18
  • View blog
  • Posts: 531
  • Joined: 04-August 09

Autonumbers in access

Posted 08 March 2010 - 10:54 AM

When I delete record in a db the ID(autonumber) doesn't change automatically

How can I do that

My delete code is

rs.Open "select * from Emp where EID=" & x & "", db, adOpenDynamic, adLockOptimistic

rs.Delete
rs.Update

rs.Close


As example
if I have IDs : (1,2,3,4,5,6)
when I delete record whose ID = 2
it should look like (1,2,3,4,5)
but what happens is (1,3,4,5,6)

This post has been edited by Ahmedn1: 08 March 2010 - 10:58 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Autonumbers in access

#2 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 458
  • View blog
  • Posts: 4,222
  • Joined: 25-March 09

Re: Autonumbers in access

Posted 08 March 2010 - 12:52 PM

the ID will never change it is ID witch mean that it will always continue from the last one even if you delete it. that is why the ID stand for. it is unique. this rly is a Data base question post it in this forum for more DB questions:
Databases

good luck :)

This post has been edited by NoBrain: 08 March 2010 - 12:54 PM

Was This Post Helpful? 0
  • +
  • -

#3 vb5prgrmr  Icon User is offline

  • D.I.C Lover

Reputation: 109
  • View blog
  • Posts: 1,016
  • Joined: 21-March 09

Re: Autonumbers in access

Posted 09 March 2010 - 12:12 AM

If it is the last record deleted, you can use a compact/repair on it and access will reset the autonumber so that when you add a new recored there is not a gap in the numbers. Otherwise it is like NoBrain said... However! If you delete a middle record (as in your example with ids from 1 to 6 and you delete #3) you can get around this and reset the autonumber so that your records have a continous id (1 to 5) if you so wish. BUT! If this ID is used in a primary key foreign key relationship, then you will break all referential integrity rules and ruin you database.

So how do you do this you ask? Well you create a temp table with the same structure and select into that table with the information from the other table less the ID field. Then you can do one of two things. Delete/Drop the old table and then do an update table and change the table name to the name of the old table, or delete the contents of the table, do a compact/repair, move the records back, and finally delete the temp table...



Good Luck
Was This Post Helpful? 0
  • +
  • -

#4 Ahmedn1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 18
  • View blog
  • Posts: 531
  • Joined: 04-August 09

Re: Autonumbers in access

Posted 10 March 2010 - 04:59 AM

good point NoBrain
I didn't think of that
Yes it is logically not useful

and thanks for help vb5
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1