update number field automatically?

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
 
D

Douglas J. Steele

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.
 
P

Patttt

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
 
J

Jeff Boyce

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
 
J

John W. Vinson

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
 
D

Douglas J. Steele

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top