I need to identify each record in my table with a unique number which
includes the year. So I want to identify the year (Format(Now(),"yy") but
then I need an Autonumber after the year. What is the syntax? I could not
figure it out?
Thanks for any help.
I'd suggest using TWO fields, not one - it's much easier to do the
custom counter code. If you wish to use just one field, though, you'll
need code like this in your Form's Beforeinsert event (you MUST use a
Form, tables don't have any usable events). I'll assume you have a
table named MyTable, with a field named MyID, text, looking like
05-00001 for the first record in 2005:
Private Sub Form_BeforeInsert(Cancel as Integer)
Dim iMax As Integer
Dim strID As String
strID = DMax("[MyID]", "[MyTable]") ' find biggest existing ID
iMax = Val(Mid(strID, 4) ' extract numeric portion
If Format(Date, "yy") > Left(strID, 2) Then ' new year?
iMax = 0
End If
Me!MyID = Format(Date, "yy-") & Format(iMax + 1, "00000")
End Sub
Needs error checking (e.g. to handle the 100000th record in a year),
etc. but should get you started.
John W. Vinson[MVP]