Re: Self-generating Number
- From: "Rick Brandt" <rickbrandt2@xxxxxxxxxxx>
- Date: Wed, 22 Oct 2008 06:25:58 -0500
trafalgar1978@xxxxxxxxx wrote:
Good evening everyone,
I built a database to track reprocessing of parts that were fixed
where I work. We are required by instruction to serialize these parts
as they come back in the following format. Last 2 digits of the year
and then the # it came back as. An example of the Serial # is
08-0122. I want to make the database automatically add to this
number
and generate a new one each time a new record is created. i have
tried
several different ways to accomplish this but nothing has worked thus
far. Any help on this issue would be greatly appreciated.
Traf
Best would be to *store* the value in two fields. A RecordDate field and a
RecordID field. You can then use an expression to *display* a value that
uses both fields as YY-0000.
This makes is much easier and much more efficient to calculate the next
numeric value when a record is created. To do so with a string field that
contains the single value "YY-0000" will be more difficult and slower.
With the two field approach you just use this for display...
=Format(RecordDate, "YY-") & Format(RecordID,"0000")
You set RecordDate's DefaultValue property to =Now() and to calculate the
next available value in a new record you use the BeforeUpdate event of the
form...
If Me.NewRecord Then
Me.RecordID = Nz(DMax("RecordID", "TableName", "Year(RecordDate) =
Year(Date())"), 0) + 1
End If
Broken down the expression finds the highest existing RecordID for records
created this year and adds 1 to it. Zero is substituted for the lookup to
account for the first record of each new year.
The WHERE argument in the above example was used because it is shorter and
easier to understand. A more verbose, but more efficient WHERE argument
would be...
"RecordDate >= DateSerial(Year(Date()), 1, 1) AND RecordDate <
DateSerial(Year(Date()) + 1, 1,1)"
--
Rick Brandt, Microsoft Access MVP
Email (as appropriate) to...
RBrandt at Hunter dot com
.
- References:
- Self-generating Number
- From: trafalgar1978@xxxxxxxxx
- Self-generating Number
- Prev by Date: Replace Multiple Substrings
- Next by Date: Re: Self-generating Number
- Previous by thread: Re: Self-generating Number
- Next by thread: Re: Self-generating Number
- Index(es):
Relevant Pages
|