update number field automatically?

  • Thread starter Thread starter Patttt
  • Start date Start date
P

Patttt

I have an existing table that has a number field as the primary key. I want
it to replicate the autonumber feature by adding the next number in sequence
when a new record is added, but I can't change it to an autonumber field and
keep the existing data. Can it be done? Thanks! Pat
 
Create a new table identical to your existing table, except make the number
field an AutoNumber.

Append the existing table to the new table.

The next value for the AutoNumber field will be one more than the largest
value already used.
 
Thanks for your quick reply, Doug. Unfortunately, the original data starts
with number 13, and if I do it this way, it starts with number 1. Is there a
way to get an autonumber field to start with #13? Pat
 
The Access Autonumber data type is designed to provide a unique row
identifier. It is not guaranteed to be sequential, and it is generally
unfit for human consumption.

If you need a guaranteed-sequential numbering system, you'll have to create
a procedure that finds the current maximum number and adds one.
Fortunately, this is fairly simple, and you can find examples search on-line
for "custom autonumber".

Good luck!

Regards

Jeff Boyce
Microsoft Office/Access MVP
 
I have an existing table that has a number field as the primary key. I want
it to replicate the autonumber feature by adding the next number in sequence
when a new record is added, but I can't change it to an autonumber field and
keep the existing data. Can it be done? Thanks! Pat

You can use a bit of VBA code in the Form's BeforeInsert event (yes, you must
use a Form, table datasheets have no usable events):

Private Sub Form_BeforeInsert(Cancel as Integer)
Me!IDField = NZ(DMax("[IDField]", "[tablename]")) + 1
End Sub
 
As I wrote, you need to append the original data to the new table before you
start using it.

Once the row with number 13 is in the table, the next number assigned should
be 14.
 
Back
Top