By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,929 Members | 1,561 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,929 IT Pros & Developers. It's quick & easy.

How to create autonumber

P: n/a
May anyone can teach me how to assign a autonumber, I want to create a
number that is starting with year(auto change to year 2006) +
autonumber (eg. 2005-0001, 2005-0002)
Nov 13 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Apple wrote:
May anyone can teach me how to assign a autonumber, I want to create a
number that is starting with year(auto change to year 2006) +
autonumber (eg. 2005-0001, 2005-0002)


Have two separate fields one with DateCreated defaulting to Now() and a
second for RecordID. Even if you don't think you need the record creation
date down to the second that could change down the road and you will be set
if it does. In in the mean time it is an easy way to get the year that you
want. With two fields it is easier to calculate the next ordinal number and
it is easy to display them on your forms and reports in the format you want
with...

=Format(DateCreated, "yyyy-") & Format(RecordID, "0000")

To assign the RecordID as each record is created use the following in the
form's BeforeUpdate event.

If IsNull(Me.RecordID) = True Then
Me.RecordID = Nz(DMax("RecordID", "TabelName" "Year(DateCreated) =
Year(Date())"), 0) + 1
End If

The above finds the highest existing RecordID value in the table created in
the same year as the current year and then adds 1 to it. The Nz() covers
the very first record of each new year and the If-Then block is necessary
because BeforeUpdate can fire more than once for a given record. This IS
the event you want to use though because it is the only one where the record
is saved immediately after assigning the new value.
--
I don't check the Email account attached
to this message. Send instead to...
RBrandt at Hunter dot com
Nov 13 '05 #2

P: n/a
"Rick Brandt" <ri*********@hotmail.com> wrote in message news:<35*************@individual.net>...
Apple wrote:
May anyone can teach me how to assign a autonumber, I want to create a
number that is starting with year(auto change to year 2006) +
autonumber (eg. 2005-0001, 2005-0002)


Have two separate fields one with DateCreated defaulting to Now() and a
second for RecordID. Even if you don't think you need the record creation
date down to the second that could change down the road and you will be set
if it does. In in the mean time it is an easy way to get the year that you
want. With two fields it is easier to calculate the next ordinal number and
it is easy to display them on your forms and reports in the format you want
with...

=Format(DateCreated, "yyyy-") & Format(RecordID, "0000")

To assign the RecordID as each record is created use the following in the
form's BeforeUpdate event.

If IsNull(Me.RecordID) = True Then
Me.RecordID = Nz(DMax("RecordID", "TabelName" "Year(DateCreated) =
Year(Date())"), 0) + 1
End If

The above finds the highest existing RecordID value in the table created in
the same year as the current year and then adds 1 to it. The Nz() covers
the very first record of each new year and the If-Then block is necessary
because BeforeUpdate can fire more than once for a given record. This IS
the event you want to use though because it is the only one where the record
is saved immediately after assigning the new value.


Thank you Brandt. I have tried your suggestion, when I input in FORM -
PROPERTY - FORMAT the followings : "=Format(DateCreated,
"yyyy-")........, it will turn to
("=F"\m"at)d"at"ec\re"at"ed",yyyy-)&f"\rm"at(R"eco\rd\ID",0000)"

Please tell me what's wrong with this?
Nov 13 '05 #3

P: n/a
"Apple" <tt*********@yahoo.com.hk> wrote in message
news:55**************************@posting.google.c om...
"Rick Brandt" <ri*********@hotmail.com> wrote in message
news:<35*************@individual.net>...
Apple wrote:
> May anyone can teach me how to assign a autonumber, I want to create a
> number that is starting with year(auto change to year 2006) +
> autonumber (eg. 2005-0001, 2005-0002)


Have two separate fields one with DateCreated defaulting to Now() and a
second for RecordID. Even if you don't think you need the record creation
date down to the second that could change down the road and you will be
set
if it does. In in the mean time it is an easy way to get the year that
you
want. With two fields it is easier to calculate the next ordinal number
and
it is easy to display them on your forms and reports in the format you
want
with...

=Format(DateCreated, "yyyy-") & Format(RecordID, "0000")

To assign the RecordID as each record is created use the following in the
form's BeforeUpdate event.

If IsNull(Me.RecordID) = True Then
Me.RecordID = Nz(DMax("RecordID", "TabelName" "Year(DateCreated) =
Year(Date())"), 0) + 1
End If

The above finds the highest existing RecordID value in the table created
in
the same year as the current year and then adds 1 to it. The Nz() covers
the very first record of each new year and the If-Then block is necessary
because BeforeUpdate can fire more than once for a given record. This IS
the event you want to use though because it is the only one where the
record
is saved immediately after assigning the new value.


Thank you Brandt. I have tried your suggestion, when I input in FORM -
PROPERTY - FORMAT the followings : "=Format(DateCreated,
"yyyy-")........, it will turn to
("=F"\m"at)d"at"ec\re"at"ed",yyyy-)&f"\rm"at(R"eco\rd\ID",0000)"

Please tell me what's wrong with this?


Rick wasn't suggesting you put that in the Format property. You put the
formula he gave into the text box's ControlSource property.

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

Nov 13 '05 #4

P: n/a
Apple,
What I do is keep a one row table with a column for each seperate sequence
number I need. If I only need one then it's only a one row, one column
table. I store the last sequence number used in that table then run an
update statement to iterate that number each time I need the next number.
This way more than one copy of the application can be runing and get the
next sequence number as needed.

"Apple" <tt*********@yahoo.com.hk> wrote in message
news:55**************************@posting.google.c om...
"Rick Brandt" <ri*********@hotmail.com> wrote in message
news:<35*************@individual.net>...
Apple wrote:
> May anyone can teach me how to assign a autonumber, I want to create a
> number that is starting with year(auto change to year 2006) +
> autonumber (eg. 2005-0001, 2005-0002)


Have two separate fields one with DateCreated defaulting to Now() and a
second for RecordID. Even if you don't think you need the record creation
date down to the second that could change down the road and you will be
set
if it does. In in the mean time it is an easy way to get the year that
you
want. With two fields it is easier to calculate the next ordinal number
and
it is easy to display them on your forms and reports in the format you
want
with...

=Format(DateCreated, "yyyy-") & Format(RecordID, "0000")

To assign the RecordID as each record is created use the following in the
form's BeforeUpdate event.

If IsNull(Me.RecordID) = True Then
Me.RecordID = Nz(DMax("RecordID", "TabelName" "Year(DateCreated) =
Year(Date())"), 0) + 1
End If

The above finds the highest existing RecordID value in the table created
in
the same year as the current year and then adds 1 to it. The Nz() covers
the very first record of each new year and the If-Then block is necessary
because BeforeUpdate can fire more than once for a given record. This IS
the event you want to use though because it is the only one where the
record
is saved immediately after assigning the new value.


Thank you Brandt. I have tried your suggestion, when I input in FORM -
PROPERTY - FORMAT the followings : "=Format(DateCreated,
"yyyy-")........, it will turn to
("=F"\m"at)d"at"ec\re"at"ed",yyyy-)&f"\rm"at(R"eco\rd\ID",0000)"

Please tell me what's wrong with this?

Nov 13 '05 #5

P: n/a
"Douglas J. Steele" <NOSPAM_djsteele@NOSPAM_canada.com> wrote in message news:<2N********************@rogers.com>...
"Apple" <tt*********@yahoo.com.hk> wrote in message
news:55**************************@posting.google.c om...
"Rick Brandt" <ri*********@hotmail.com> wrote in message
news:<35*************@individual.net>...
Apple wrote:
> May anyone can teach me how to assign a autonumber, I want to create a
> number that is starting with year(auto change to year 2006) +
> autonumber (eg. 2005-0001, 2005-0002)

Have two separate fields one with DateCreated defaulting to Now() and a
second for RecordID. Even if you don't think you need the record creation
date down to the second that could change down the road and you will be
set
if it does. In in the mean time it is an easy way to get the year that
you
want. With two fields it is easier to calculate the next ordinal number
and
it is easy to display them on your forms and reports in the format you
want
with...

=Format(DateCreated, "yyyy-") & Format(RecordID, "0000")

To assign the RecordID as each record is created use the following in the
form's BeforeUpdate event.

If IsNull(Me.RecordID) = True Then
Me.RecordID = Nz(DMax("RecordID", "TabelName" "Year(DateCreated) =
Year(Date())"), 0) + 1
End If

The above finds the highest existing RecordID value in the table created
in
the same year as the current year and then adds 1 to it. The Nz() covers
the very first record of each new year and the If-Then block is necessary
because BeforeUpdate can fire more than once for a given record. This IS
the event you want to use though because it is the only one where the
record
is saved immediately after assigning the new value.


Thank you Brandt. I have tried your suggestion, when I input in FORM -
PROPERTY - FORMAT the followings : "=Format(DateCreated,
"yyyy-")........, it will turn to
("=F"\m"at)d"at"ec\re"at"ed",yyyy-)&f"\rm"at(R"eco\rd\ID",0000)"

Please tell me what's wrong with this?


Rick wasn't suggesting you put that in the Format property. You put the
formula he gave into the text box's ControlSource property.

Thank you Doug, I had tried to put the formula into FORM-TEXT
BOX-CONTROL SOURCE PROPERTY or FORM-UNBOUNDED TEST BOX-CONTROL SOURCE
PROPERTY, but it show error(#xxxx?); when I tried to put into a query
as an expression, when open up the query or form, it will ask to enter
an parameter (1) date; (2) number, if I entre (1) 2005/01/01; (2)
0001, it will show 2005-0001 in all record that I have input data
before.

The following is in my form's BeforeUpdate event :

Private Sub RecordID_BeforeUpdate(Cancel As Integer)
If IsNull(Me.RecordID) = True Then
Me.RecordID = Nz(DMax("RecordID", "FORM
NAME""Year(DateCreated)=Year(Date())"), 0) + 1
End If
End Sub
Please comment!!
Nov 13 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.