I am trying to set up a table that will use a 6 digit consecutive number
with a letter prefix as the key. I've got the format figured out, but how
do I start the numbering at say N100500 rather then N000001 ?
TIA!
Well, I'd say DON'T use an Access autonumber. For one thing they cannot
contain letters; for another, they can and will develop gaps in the number
system, and can become random (if you replicate your database).
If the N is constant (all records will have N, never any other letter) then
don't store it in your table at all. Instead, use a Format to just *display*
the letter in conjunction with a Long Integer number as the key. If it is
variable (e.g. you'll have N002154 and K000052 and G000052 as different valid
keys), use two fields, one for the letter, one for the number, and concatenate
them for display.
To increment the number you can enforce all data entry to be done using a
Form, and put code in the form's BeforeInsert event like
Private Sub Form_BeforeInsert(Cancel as Integer)
Dim iNext As Long
iNext = DMax("[ID]", "[yourtablename]")
If iNext >= 999999 Then
MsgBox "Shut down the computer, go home. No more ID's available."
Else
Me!txtID = INext+1
Me.Dirty = False ' write the record to disk
End If
End Sub
If you have a multiuser system, this is a LITTLE bit risky as it's just
possible that one user will be inserting a record at the same time as this
code is running and you'll get duplicate value errors. There are ways around
this but they involve more code.