473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Generating Numbers automatically

I'm using Access 2002.
I have a field called Job No.
It is a text (data type). The reason being that there is an "I" in front of
each number i.e. I1234 I1235 etc
The problem is that when the user enters a new record, he/she has to know
what the last recorded job no. is present in the database, in order to then
assign the next sequential job no. So its a manual process.
I don't want to seperate the "I" component from the Job No. field in order
the make the field an autonumber datatype.
Is there some code I could use to allow the system to somehow automatically
assign the next sequential number incorporating the prefix "I" to each
number ?
thank you for any help.
Nov 13 '05 #1
11 2501
Gary wrote:
I'm using Access 2002.
I have a field called Job No.
It is a text (data type). The reason being that there is an "I" in
front of each number i.e. I1234 I1235 etc
The problem is that when the user enters a new record, he/she has to
know what the last recorded job no. is present in the database, in
order to then assign the next sequential job no. So its a manual
process.
I don't want to seperate the "I" component from the Job No. field in
order the make the field an autonumber datatype.
Is there some code I could use to allow the system to somehow
automatically assign the next sequential number incorporating the
prefix "I" to each number ?
thank you for any help.


DMax("Mid(FieldName,2)", "TableName")

Should retrieve the highest value, but since it is having to use the Mid()
function first it is going to be inefficient as hell. You REALLY should
make the "I" separate and in fact if it is always an "I" it doesn't even
need to be stored in the table at all. You could store just the number and
use a format setting to *display* it with an "I" in the front while using an
actual number field in which case DMax("FieldName", "TableName") will be
much more efficient. Particularly if you add a descending index to the
field.

--
I don't check the Email account attached
to this message. Send instead to...
RBrandt at Hunter dot com
Nov 13 '05 #2
Gary wrote:
I'm using Access 2002.
I have a field called Job No.
It is a text (data type). The reason being that there is an "I" in
front of each number i.e. I1234 I1235 etc
The problem is that when the user enters a new record, he/she has to
know what the last recorded job no. is present in the database, in
order to then assign the next sequential job no. So its a manual
process.
I don't want to seperate the "I" component from the Job No. field in
order the make the field an autonumber datatype.
Is there some code I could use to allow the system to somehow
automatically assign the next sequential number incorporating the
prefix "I" to each number ?
thank you for any help.


DMax("Mid(FieldName,2)", "TableName")

Should retrieve the highest value, but since it is having to use the Mid()
function first it is going to be inefficient as hell. You REALLY should
make the "I" separate and in fact if it is always an "I" it doesn't even
need to be stored in the table at all. You could store just the number and
use a format setting to *display* it with an "I" in the front while using an
actual number field in which case DMax("FieldName", "TableName") will be
much more efficient. Particularly if you add a descending index to the
field.

--
I don't check the Email account attached
to this message. Send instead to...
RBrandt at Hunter dot com
Nov 13 '05 #3
good point. gary

Nov 13 '05 #4
good point. gary

Nov 13 '05 #5
Thanks for the advice. I think I will simply go with the number and remove
the "I" as you suggested.
I actually added +1 to that formula - eg: DMax("FieldName", "TableName")+1
to give me the next sequential number.
Basically i have the frm_main which is the viewing of all records. The
addition of new records is done in frm_dataentry.
In this frm_dataentry, I added that Dmax formula (in the JobNo field - in
the Controlsource section). It does give me the next sequential number when
I go into the data entry form, but when I go back to the main form (after
completing the dataentry form with info) to see if it was saved, it did save
all the data previously entered in the dataentry form but not the JobNo -
that was given to me automatically while in the data entry form. That field
was simply blank.
"Rick Brandt" <ri*********@hotmail.com> wrote in message
news:6l**************@newssvr27.news.prodigy.net.. .
Gary wrote:
I'm using Access 2002.
I have a field called Job No.
It is a text (data type). The reason being that there is an "I" in
front of each number i.e. I1234 I1235 etc
The problem is that when the user enters a new record, he/she has to
know what the last recorded job no. is present in the database, in
order to then assign the next sequential job no. So its a manual
process.
I don't want to seperate the "I" component from the Job No. field in
order the make the field an autonumber datatype.
Is there some code I could use to allow the system to somehow
automatically assign the next sequential number incorporating the
prefix "I" to each number ?
thank you for any help.
DMax("Mid(FieldName,2)", "TableName")

Should retrieve the highest value, but since it is having to use the Mid()
function first it is going to be inefficient as hell. You REALLY should
make the "I" separate and in fact if it is always an "I" it doesn't even
need to be stored in the table at all. You could store just the number

and use a format setting to *display* it with an "I" in the front while using an actual number field in which case DMax("FieldName", "TableName") will be
much more efficient. Particularly if you add a descending index to the
field.

--
I don't check the Email account attached
to this message. Send instead to...
RBrandt at Hunter dot com

Nov 13 '05 #6
Thanks for the advice. I think I will simply go with the number and remove
the "I" as you suggested.
I actually added +1 to that formula - eg: DMax("FieldName", "TableName")+1
to give me the next sequential number.
Basically i have the frm_main which is the viewing of all records. The
addition of new records is done in frm_dataentry.
In this frm_dataentry, I added that Dmax formula (in the JobNo field - in
the Controlsource section). It does give me the next sequential number when
I go into the data entry form, but when I go back to the main form (after
completing the dataentry form with info) to see if it was saved, it did save
all the data previously entered in the dataentry form but not the JobNo -
that was given to me automatically while in the data entry form. That field
was simply blank.
"Rick Brandt" <ri*********@hotmail.com> wrote in message
news:6l**************@newssvr27.news.prodigy.net.. .
Gary wrote:
I'm using Access 2002.
I have a field called Job No.
It is a text (data type). The reason being that there is an "I" in
front of each number i.e. I1234 I1235 etc
The problem is that when the user enters a new record, he/she has to
know what the last recorded job no. is present in the database, in
order to then assign the next sequential job no. So its a manual
process.
I don't want to seperate the "I" component from the Job No. field in
order the make the field an autonumber datatype.
Is there some code I could use to allow the system to somehow
automatically assign the next sequential number incorporating the
prefix "I" to each number ?
thank you for any help.
DMax("Mid(FieldName,2)", "TableName")

Should retrieve the highest value, but since it is having to use the Mid()
function first it is going to be inefficient as hell. You REALLY should
make the "I" separate and in fact if it is always an "I" it doesn't even
need to be stored in the table at all. You could store just the number

and use a format setting to *display* it with an "I" in the front while using an actual number field in which case DMax("FieldName", "TableName") will be
much more efficient. Particularly if you add a descending index to the
field.

--
I don't check the Email account attached
to this message. Send instead to...
RBrandt at Hunter dot com

Nov 13 '05 #7
Gary wrote:
Thanks for the advice. I think I will simply go with the number and
remove the "I" as you suggested.
I actually added +1 to that formula - eg: DMax("FieldName",
"TableName")+1 to give me the next sequential number.
Basically i have the frm_main which is the viewing of all records. The
addition of new records is done in frm_dataentry.
In this frm_dataentry, I added that Dmax formula (in the JobNo field
- in the Controlsource section). It does give me the next sequential
number when I go into the data entry form, but when I go back to the
main form (after completing the dataentry form with info) to see if
it was saved, it did save all the data previously entered in the
dataentry form but not the JobNo - that was given to me automatically
while in the data entry form. That field was simply blank.


You can't use an expression as a ControlSouce if you want the value saved. The
only time a control's value is saved to the table is when it has a ControlSource
that is the name of a field (and nothing else).

You need to set the ControlSource to your field and then in the BeforeUpdate
event of your Form have code...

If Nz(Me.JobNo, 0) = 0 Then
Me.JobNo = Nz(DMax("JobNo", "TableName"), 0) + 1
End If

You could (in theory) use the DMax() in your DefaultValue property or assign it
in a different form event, but those options do not work reliably in a
Multi-User environment. BeforeUpdate is the only event where the calculation of
the next number and the saving of the record happen almost at the same time so
that there is (almost) no chance of two users grabbing the same value.

--
I don't check the Email account attached
to this message. Send instead to...
RBrandt at Hunter dot com
Nov 13 '05 #8
Gary wrote:
Thanks for the advice. I think I will simply go with the number and
remove the "I" as you suggested.
I actually added +1 to that formula - eg: DMax("FieldName",
"TableName")+1 to give me the next sequential number.
Basically i have the frm_main which is the viewing of all records. The
addition of new records is done in frm_dataentry.
In this frm_dataentry, I added that Dmax formula (in the JobNo field
- in the Controlsource section). It does give me the next sequential
number when I go into the data entry form, but when I go back to the
main form (after completing the dataentry form with info) to see if
it was saved, it did save all the data previously entered in the
dataentry form but not the JobNo - that was given to me automatically
while in the data entry form. That field was simply blank.


You can't use an expression as a ControlSouce if you want the value saved. The
only time a control's value is saved to the table is when it has a ControlSource
that is the name of a field (and nothing else).

You need to set the ControlSource to your field and then in the BeforeUpdate
event of your Form have code...

If Nz(Me.JobNo, 0) = 0 Then
Me.JobNo = Nz(DMax("JobNo", "TableName"), 0) + 1
End If

You could (in theory) use the DMax() in your DefaultValue property or assign it
in a different form event, but those options do not work reliably in a
Multi-User environment. BeforeUpdate is the only event where the calculation of
the next number and the saving of the record happen almost at the same time so
that there is (almost) no chance of two users grabbing the same value.

--
I don't check the Email account attached
to this message. Send instead to...
RBrandt at Hunter dot com
Nov 13 '05 #9
On Sat, 16 Jul 2005 23:04:20 +1000, "Gary" <g4****@hotmail.com> wrote:
Thanks for the advice. I think I will simply go with the number and remove
the "I" as you suggested.
I actually added +1 to that formula - eg: DMax("FieldName", "TableName")+1
to give me the next sequential number.


if you follow this route you might just as well use autonumber rather
than DMax. I think this is what Rick was suggesting. The autonumber
route is more reliable if several users can add records at the same
time.

The two methods behave differently if records with the highest numbers
are removed - DMax will reuse the numbers whereas Autonumber will only
do this if the database is compacted before any more records are
added.

Nov 13 '05 #10
On Sat, 16 Jul 2005 23:04:20 +1000, "Gary" <g4****@hotmail.com> wrote:
Thanks for the advice. I think I will simply go with the number and remove
the "I" as you suggested.
I actually added +1 to that formula - eg: DMax("FieldName", "TableName")+1
to give me the next sequential number.


if you follow this route you might just as well use autonumber rather
than DMax. I think this is what Rick was suggesting. The autonumber
route is more reliable if several users can add records at the same
time.

The two methods behave differently if records with the highest numbers
are removed - DMax will reuse the numbers whereas Autonumber will only
do this if the database is compacted before any more records are
added.

Nov 13 '05 #11
But how do we do compact programatically?

Nov 13 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jack Higgs | last post by:
I'm building a maths program for year 6 children. Problem with division - I generate a random number between say 1 and 1000. I want to generate another random number which when divided by the...
7
by: Steve Jorgensen | last post by:
Here is some code to generate code for raising and getting information about custom errors in an application. Executing the GenerateXyzErrDefs procedure generates the code. <tblXyzError>...
0
by: Gary | last post by:
I'm using Access 2002. I have a field called Job No. It is a text (data type). The reason being that there is an "I" in front of each number i.e. I1234 I1235 etc The problem is that when...
14
by: Anthony Liu | last post by:
I am at my wit's end. I want to generate a certain number of random numbers. This is easy, I can repeatedly do uniform(0, 1) for example. But, I want the random numbers just generated sum up...
8
by: kiranchahar | last post by:
Hey all, How do I generate random numbers with Uniform distribution Uniform(a,b) using C-programming? I want to generate uniform random numbers which have mean following Uniform(p,q) and also...
1
by: sparks | last post by:
we are generating numbers for the data collection people. this worked fine for a while.. Private Sub generatenumber() Dim db As DAO.Database Dim rs As DAO.Recordset Dim StudentNumber As...
26
by: bilgekhan | last post by:
What is the correct method for generating 2 independent random numbers? They will be compared whether they are equal. What about this method: srand(time(0)); int r1 = rand(); srand(rand());...
3
by: mphil.star | last post by:
I am creating a certain program and in that program i want to add a field which will be creating numbers with text in sequence older " Like MPL 001/08 to unlimited " And 08 be current year. This...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.