473,569 Members | 2,793 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2519
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(Field Name,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(Field Name,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*********@ho tmail.com> wrote in message
news:6l******** ******@newssvr2 7.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(Field Name,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*********@ho tmail.com> wrote in message
news:6l******** ******@newssvr2 7.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(Field Name,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

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

Similar topics

2
6366
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 first number gives a whole number answer. Any Idea how I could do this? Obviously generating prime numbers is a bit of a problem as well! Cheers, ...
7
2697
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> ErrorCodeOffset (Long Int) ErrorObjBaseName (Text(50)) ErrorDescription (Text(255)) 1 DontDoThat Hey! Don't do that!
0
341
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 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...
14
9866
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 to 1 . I am not sure how to do this. Any idea? Thanks.
8
25443
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 variance as Uniform(s,t)? any suggestion would be really appreciated. Thanks, Kay
1
1570
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 Integer Dim gennum As Integer Dim finalnum As Integer
26
7880
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()); int r2 = rand(); bool f = r1 == r2;
3
1293
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 will only be possible if that field is enebled, if the condition of enebling this field still is false then the field returns the previous value...
0
7695
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7668
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6281
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1209
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.