473,385 Members | 1,872 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,385 software developers and data experts.

Customer Autonumber increments?

Hi,

I know this question is like a broken record but I cannot find a simple answer to follow, I am new to Access/VBA but I can find my way round so I do not have a problem with following instructions so I hope someone will have the time to help me out on this one.

Here goes, I have a table with a number of fields as follows
Job Number <---- Auto Number and Primary Key
Customer
Work Done


I need to make sure that each job number is increment and also a unique key which cannot be duplicated. Now when I use the Access Autonumber it goes stupid if I accidently add a new record to a form and press escape. so when I decide to add another record the sequence is gone.

I know this cannot be done on a table level, So it needs to be done on a form level, is there a way to create a method to increment the numbers based on the last record?

I would appreciate the help and I am desperate :(

Thank you
Sam
Sep 8 '10 #1

✓ answered by TheSmileyCoder

Hi, yes there is missing a Then part.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2.   If Me.NewRecord Then 'Only do following if its a newrecord
  3.     Me.tb_ID=Nz(Dmax("[ID Field]","[tbl Whatever]"),0)+1
  4.   End If
  5. End Sub
If your tables (and/or fields) have spaces you should enclose them in square brackets.It is also correct that you should use the Nz function to guard against the case of no result being returned (if its the first record in the table)

As a general rule, I personally always avoid using spaces in my table and field names.

NeoPa, you're correct it could also be placed in BeforeInsert. I personally just have alot of code running in the beforeUpdate and prefer to keep the code gathered there. Either way will work though.
NeoPa:
Please ignore this as it turned out I wasn't correct at all - as explained in post #5.

15 4927
liimra
119 100+
DMAX is the solution. Easy, Simple and straight forward.

Change the Autonumber field to Normal Number field. and make the default value for that field
Expand|Select|Wrap|Line Numbers
  1. = DMAX ("NumberFieldNAME","TableNAME") + 1
Of course there are many workarounds to set the DMAX number through Macros or VB (On Load, After update..etc); but the simplest is the default value.

If the table is new and there is no least number, the above expression will
give no outcome. that is why you use the NZ:

= NZ(DMAX ("NumberFieldNAME","TableNAME"),0) + 1

Now, if there is no value, the value given will be 1.

Please change to given names.


Regards,
Ali
Sep 8 '10 #2
NeoPa
32,556 Expert Mod 16PB
This question is unusually asked clearly. You're right in that it's asked quite frequently. It's rarely asked clearly though. This is a pleasant exception.

As far as it goes Ali's answer is good. It's well explained and covers the basic concepts.

What may be additionally required though, but only in some circumstances, is the handling of this in multi-user environments. In such an environment, or for when developing work which may be used that way in future, it is important only to check the table and prepare a value at the point of writing away the record (In the Form_BeforeInsert() event procedure).

This does mean that the value cannot be shown to the operator beforehand. Many find this strange and un-user friendly, but that's only because they don't properly grasp the concepts. You'll never see the value of an AutoNumber showing until the record is created. That's because until the record is created it doesn't exist. If you keep to this design you won't go far wrong.

Lastly, the sequence numbers cannot be guaranteed if records are removed after new ones have been created. This only guarantees that the numbers are created in the correct sequence.
Sep 8 '10 #3
TheSmileyCoder
2,322 Expert Mod 2GB
One problem with the DefaultValue approach is that defaultValues are "loaded" when the record starts being edited. This means that if user A starts record 10 (9 was the last saved record) and user B starts a record before user A saves, both records will be 10 (and the save will fail, with a message about creating duplicate values in index.)

I would recommend doing it in the Forms Before update event. If you select your FORM's properties (click top left box of your form) and select Events, go for BeforeUpdate. Click the 3 ... on the right hand side, and select Event Procedure and you will be shown a Visual Basic for Applications (VBA) editor window. Access will have filled in some information for you allready, looking like this:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2.  
  3. End Sub
This is the event access will run before a update is performed. Now add following:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2.   If Me.NewRecord 'Only do following if its a newrecord
  3.      Me.tb_ID=Dmax("ID_Field","tbl_Whatever")+1
  4.   End If
  5. End Sub
Where tb_ID is the name of a textbox on your field, with its controlsource being your primary key of type Number, Long (so not autonumber)
Sep 8 '10 #4
NeoPa
32,556 Expert Mod 16PB
Bizarre! The time of your post appears on my screen as earlier than the one on mine, yet it appears afterwards in the list. Maybe you edited it or something.

Anyway, a good point Smiley, but I would draw your attention to a couple of points :
  1. The Nz() part of Ali's post. This needs to be included.
  2. Although it's possible to use a Before... event procedure, it's more logical to use an After... one. No it isn't!! What am I on about. My solution should have used Form_BeforeInsert(). I'm glad you posted. It made me think properly.
  3. Form_BeforeInsert() is preferable to Form_BeforeUpdate() as it already works only on new records (you needn't add the extra code).
    Forget this point! BeforeInsert Event [Access 2003 VBA Language Reference] explains at which point the event is triggered and it's not where I thought. Nor is it appropriate for this.

It seems Smiley had more right than I did all along :-(
Sep 8 '10 #5
I tried this method but I am getting an error, is that because I cannot use spaces in the names and I need to use under scores?

Should I change the Table name and field name to names with out Spaces?
Sep 9 '10 #6
Is there some code missing from the IF command such as then?
Sep 9 '10 #7
TheSmileyCoder
2,322 Expert Mod 2GB
Hi, yes there is missing a Then part.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2.   If Me.NewRecord Then 'Only do following if its a newrecord
  3.     Me.tb_ID=Nz(Dmax("[ID Field]","[tbl Whatever]"),0)+1
  4.   End If
  5. End Sub
If your tables (and/or fields) have spaces you should enclose them in square brackets.It is also correct that you should use the Nz function to guard against the case of no result being returned (if its the first record in the table)

As a general rule, I personally always avoid using spaces in my table and field names.

NeoPa, you're correct it could also be placed in BeforeInsert. I personally just have alot of code running in the beforeUpdate and prefer to keep the code gathered there. Either way will work though.
NeoPa:
Please ignore this as it turned out I wasn't correct at all - as explained in post #5.
Sep 9 '10 #8
liimra
119 100+
Yes Neopa is right and I totally agree to create the number just before saving the record in case you are working in multi-user environment; Though I would set the value (number) through a "Save" button.

In case you want to have all numbers in sequence, you have to update the table (query) manually or automatically everynow and then when old records are deleted because as Neopa stated
"the sequence numbers cannot be guaranteed if records are removed after new ones have been created. This only guarantees that the numbers are created in the correct sequence."

Example: Suppose you have five records with the following numbers (1,2,3,4,5); If you delete the third record, the sequence is lost because the next record you create will have number 6 (5+1). To overcome this, you change the numbers back in order manually or automatically using VB. Is that everything? Not quite, because I would really advice not to delete any record meaning the number. For example, suppose this number is used for invoices. Later you wanted to delete the third invoice because it was canceled; in this case I would advise to change the value, quantity and any other related field to null, and state the reason for canceling, while keeping the number (and the "date".)


Hope that helps,
Regards
Ali
Sep 9 '10 #9
NeoPa
32,556 Expert Mod 16PB
Smiley:
NeoPa, you're correct it could also be placed in BeforeInsert. I personally just have alot of code running in the beforeUpdate and prefer to keep the code gathered there. Either way will work though.
It certainly works your way and I can see your rationale (particularly for your own situation). Actually, including different approaches exposes the thread to more available options anyway. Me.NewRecord is an interesting one that inexperienced programmers are unlikely to know about first off.
Sep 9 '10 #10
NeoPa
32,556 Expert Mod 16PB
Sam, notwithstanding the tone of my reply in the other thread (since deleted and private) we can make this solution more understandable for you should you but explain where your confusion lies.

There are some pretty simple steps in here, but there is also a lot of other discussions around the subject and other (very similar) solutions posted. If you need help understanding which to focus on then just ask. Actually, Ali's post #2 gives a pretty clear breakdown of the concept. My post #3 explains some of the multi-user complications should that be relevant to your requirements. Smiley's post #4 gives some actual code to copy/paste if that's what you need. The code was updated later, in post #8, after some of the discussion, but seems to be what you asked for.

Reading your posts I get the impression of someone who can express themself clearly. Let us know what is missing if you still feel it is.
Sep 9 '10 #11
thank you for everyone who helped I realised the problem, I wasn't referring to the correct field and table name, as I got confused of the format, I even managed to completely ruin the form to the point where access was spitting out an error of low memory. But it is all good and thank you for your help my problem is now solved, I know the solution is not water tight but for what I am doing it does the job. and I do apologise if I was out of line with my other post, no intention of upsetting anyone but I am out of my depth with this stuff and I should be more clear next time.
Thanks again
you guys been great.
Sam
Sep 9 '10 #12
NeoPa
32,556 Expert Mod 16PB
Good for you Sam. I'm pleased you managed to find your way through in the end, and we'll be here if you need us again :)
Sep 10 '10 #13
liimra
119 100+
Glad it worked for you. Would you kindly mark this post as answered
so it becomes more helpful for others.

Regards,
Ali
Sep 13 '10 #14
How do I mark it as answered?
Sep 14 '10 #15
NeoPa
32,556 Expert Mod 16PB
I recently found that the Form_BeforeInsert() event procedure is triggered as soon as a new record is dirtied (IE. Any data is entered into it.), and not when the new record is actually added to the record source. With that in mind I'd like to amend my post #5.
Jun 23 '11 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Alan Carter | last post by:
http://www.mvps.org/access/tencommandments.htm Thou shalt not use Autonumber if the field is meant to have meaning for thy users. Why? Alan
35
by: Traci | last post by:
If I have a table with an autonumber primary key and 100 records and I delete the last 50 records, the next record added would have a primary key of 101. Is there any way to have the primary key...
26
by: jimfortune | last post by:
Sometimes I use Autonumber fields for ID fields. Furthermore, sometimes I use those same fields in orderdetail type tables. So it's important in that case that once an autonumber key value is...
4
by: Apple | last post by:
1. I want to create an autonumber, my requirement is : 2005/0001 (Year/autonumber), which year & autonumber no. both can auto run. 2. I had create a query by making relation to a table & query,...
2
by: Nick J | last post by:
Hi, I created a database not so long back but now I am offering a better version with new features. I am having some difficulty thinking of a method to keep the data within the database...
7
by: jmar | last post by:
I'm hoping someone out there can give me a little guidance. I have an Access Database "Customer.MDB" with a table "CustInfo" that has the following design: Field: DataType CustID AutoNumber...
4
by: Lyn | last post by:
Hi, Is there an "easy" way to write a full record (all fields) using "INSERT INTO..." into a table which has an AutoNumber field? Normally, to write a full new record I would use: INSERT INTO...
4
imrosie
by: imrosie | last post by:
Hello All, (newbie)...I'm working on an Ordering app using the typical tables (customers, orders). I have a Search form to find existing customers prior to entering a new order. On the search form...
7
imrosie
by: imrosie | last post by:
Hello all, My Customer table with Autonumbers used as the customer account number. There are two main forms (Order frm, Customer frm); the Customer's account number displays in a bound control on...
6
by: ashes | last post by:
Hi, I am creating an ecommerce website using Microsoft Visual Studio, VB.Net and MS Access 2003. I am new to VB.Net When someone wants to register on the website, they fill out a form and the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.