Craig said:
I am creating a data base and want to add a formula to my table so
that when a form is filled out an automatic number is assigned to
each new document. The starting number will be 05-001. Is there a
formula I can write? Or is there another way to achieve this?
Assuming that the "05" portion is the year it would be best to store this in two
fields. One that holds the record creation date [RecordDate], and one that
holds the ordinal value [RecordID]. Then it becomes relatively easy (in a form)
to assign the appropriate values.
For RecordDate you simply set the default value property to either Date() or
Now() depending on whether you want to capture the time as well as the date. I
would recommend Now() because even if you don't think you need to know the exact
time of record creation, that requirement could change down the road and you
will already have it. This default value could be set at the table level.
For RecordID you need a code routine that will find the highest existing ordinal
value for the records created in the current year and then add one to it. I
would use the BeforeUpdate event of the form with the following code...
If IsNull(Me.RecordID) = True Then
Me.RecordID = Nz(DMax("RecordID", "TableName", "Year(RecordDate) =
Year(Date)"), 0) + 1
End If
You need to make sure that RecordID has no default setting in either the table
design or the form.
Now, *for display* on your forms and reports you use an expression of...
=Format(RecordDate,"yy-") & Format(RecordID,"000")