473,507 Members | 2,460 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Customized autonumber ID.

111 New Member
I have to create a RecordID which should be unique and autoincremented. The specifications are something like,

1) The number must be 9 digits long
2)First two digits should represent current YY, which should increment every year
3)Next 7 digits should be like 0000001 and should be autoincremented (like 7 digits long autonumber concatenated to the current year)

so the final number should look like this: 090000001, 090000002, and so on.

What should be my approach to this?
Thanks in advance as always!!
Dec 9 '09 #1
15 2781
ChipR
1,287 Recognized Expert Top Contributor
You may not be able to store preceding zeros if you use a number field, so probably a string. Here's how I think you would do it.

Get the max RecordID with DMax.
Convert the first 2 characters to an integer.
See if they are equal to the current 2 digit year.
If so,
-Convert the rest of the RecordID to an Integer.
-Add 1 (deal with overflow?)
-Create a string, set it equal to the number.
-Append "0" to the front of the string until it is the proper length of 7
-Combine it with the year string.
Else,
-Create a string with the first two digits of the current year and "0000001"
End if.
Done.
Dec 9 '09 #2
ADezii
8,834 Recognized Expert Expert
You're probably better off leaving it as Text if you can, but that being said, this simple Function should do the trick. I'll leave it up to you to figure in the possible year change:
Expand|Select|Wrap|Line Numbers
  1. Public Function fIncrementAuto() As String
  2. Dim varMax As Variant
  3.  
  4. varMax = DMax("[YourNumber]", "tblTest")
  5.  
  6. fIncrementAuto = Format$(Left(varMax, 2), "00") & Format(Mid$(varMax, 3) + 1, "0000000")
  7. End Function
Dec 9 '09 #3
AccessBeetle
111 New Member
All these coding should be placed under Form's On open_click event. Am I right?
Dec 9 '09 #4
AccessBeetle
111 New Member
sorry I did not see Adezil's answer.
Dec 9 '09 #5
missinglinq
3,532 Recognized Expert Specialist
Come on, ADezii, that's the easy part! Doing the year change thing is the fun part! This will do it all.

Note that AI_Number is defined as Text.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2.  
  3. If Me.NewRecord Then
  4.  
  5.  If RecordsetClone.RecordCount = 0 Then
  6.   Me.AI_Number = Format(Date, "yy") & "0000001"
  7.  End If
  8.  
  9.  If RecordsetClone.RecordCount <> 0 Then
  10.   If Left(DMax("AI_Number", "YourActualTableName"), 2) = Format(Date, "yy") Then
  11.     Me.AI_Number = Format(DMax("val([AI_Number])", "YourActualTableName") + 1, "000000000")
  12.   Else
  13.     Me.AI_Number = Format(Date, "yy") & "0000001"
  14.   End If
  15.  End If
  16.  
  17. End If
  18. End Sub
By placing the code in the Form_BeforeUpdate, the number is assigned at the last possible moment before the record is saved, decreasing the chances, in a multi-user environment, of two records being assigned the same number. Using this method for a number of years, I have never run into this problem.

Linq ;0)>
Dec 9 '09 #6
ADezii
8,834 Recognized Expert Expert
We have to leave something for the OP, don't we? (LOL)!
Dec 9 '09 #7
AccessBeetle
111 New Member
I am getting an error saying Cannot insert duplicate value in the table. ODBC call failed. When I tried to enter the record, the RecordsID populated with "090000001" second time also. Then I try to move to enter another record and there the error is.
Also, it would be great if you can explain me this part
Expand|Select|Wrap|Line Numbers
  1. If Left(DMax("RecordID", "RadioLog_tblRadioCallActivity"), 2) = Format(Date, "yy") Then
  2.     Me.RecordID = Format(DMax("val([RecordID])", "RadioLog_tblRadioCallActivity") + 1, "000000000")
  3.   Else
  4.     Me.RecordID = Format(Date, "yy") & "0000001"
  5.   End If
  6.  
My actual tables are in SQL 2005 and I am linking them in Access 2003. Also, the application contains many forms from where user can enter new data.
Shall I write this code under each and every form? (I think I have to but not sure)

Exact Error:
ODBC Call Failed
Violation of Primary key constraint PK_RadioLog_tbl_RadioCallActivity cannont insert duplicate key in object 'dbo.RadioLog_tblRadioCallActivity. (#2627) and (#3621)
Dec 9 '09 #8
AccessBeetle
111 New Member
OK OK.
It is solved. It was the data creating problem. I removed all the data from all the adjoining tables and table itself is also clear.
There is still something wrong. I have customized Login interface. I logged in with User1 and entered the data. It was ok. Then I logged in using User2 and started to add data and it was again starting from 090000001. There was the same error there again. What am I doing wrong?
Thanks for help
Dec 9 '09 #9
missinglinq
3,532 Recognized Expert Specialist
Not really sure! It works fine on this end, but, of course, I'm not using a SQL 2005 back end, and have no real experience with it. Hopefully someone else here doesan d will be able to help..

The bit of code you asked about is checking to see if the year portion of the last record entered is the same as the current year. IF it is, it continues incrementing the number with the same year prefix. If it isn't it assigns the new year prefix and starts the "count" over again at 0000001.

Linq ;0)>
Dec 9 '09 #10
AccessBeetle
111 New Member
MissingLinq,
Thanks for explaining. I don't think the problem is on SQL side as it does updates the value but it could not compare it with the last entered RecordID and that is why whenever there is another user trying to enter data it starts all over again..So that this error (cannot insert duplicate primary key) pops up.
any inputs on that?
There are two forms which allows data entry for the same table (one has Data Entry property set to Yes and one has Data Entry property set to No and the second one also filters data based on District). Do you think that I have to write this code under both Forms' BeforeUpdate() event?
Is there any way we can attach such kind of logic to the table's RecordID field?
Thanks
Dec 10 '09 #11
missinglinq
3,532 Recognized Expert Specialist
I don't think the problem is on SQL side as it does updates the value but it could not compare it with the last entered RecordID
How do you know this? And why wouldn't it be able to compare it?

I think you need to post your entire Form_BeforeUpdate sub here for us to look at.

Linq ;0>
Dec 10 '09 #12
AccessBeetle
111 New Member
Expand|Select|Wrap|Line Numbers
  1. If Me.NewRecord Then
  2.  
  3.  If RecordsetClone.RecordCount = 0 Then
  4.   Me.RecordID = Format(Date, "yy") & "0000001"
  5.  End If
  6.  
  7.  If RecordsetClone.RecordCount <> 0 Then
  8.   If Left(DMax("RecordID", "RadioLog_tblRadioCallActivity"), 2) = Format(Date, "yy") Then
  9.     Me.RecordID = Format(DMax("val([RecordID])", "RadioLog_tblRadioCallActivity") + 1, "000000000")
  10.   Else
  11.     Me.RecordID = Format(Date, "yy") & "0000001"
  12.   End If
  13.  End If
  14.  
  15. End If
Here is my entire sub. I am saying that because, when I logged in with UserNo1 and enter data it all works perfactly fine (suppose I entered two set of data with RecordID 09-0000001 and 09-0000002). Then I log out and log in with UserNo2. It enters first records with RecordID like "0000001" instead of incrementing it with one like "09-0000003". Then when I hit enter it gives me that error saying Cannot insert duplicate value. There is something wrong and it might be a little thing but right now it seems like huge hurdle.
Thanks for looking into it.
Dec 11 '09 #13
MrDeej
157 New Member
dim rst as new adodb.recordset
with rst
.open "SELECT * from [table]",currentproject.connection,adopenforwardonly,adlo ckoptimistic
.addnew
![customized autonumber id] = right(year,2) & format(!autonumber;"00000000")
![other field] = [other data]
.update
.close

end with
set rst = nothing
Dec 11 '09 #14
AccessBeetle
111 New Member
Thank You MeDeej for your input.
Missinglinq and every person who runs into this problem,
I figured out what was causing the problem. (Yehhhh!!) It was the form's Data Entry property. As I have said mupltiple times before that I have two forms who are bound to the same table. One has Data entry property set to yes and other one has to No. When user logs in and go to SingleLine data entry (Data entry = yes), the form assumes that there are no previous records in the table so it fails to evaluate this line of code
Expand|Select|Wrap|Line Numbers
  1. If RecordsetClone.RecordCount = 0 Then 
  2.   Me.RecordID = Format(Date, "yy") & "0000001" 
  3.  End If 
  4.  
Although, the last records entered was "09-0000002", it goes thorugh this loop and starts it again with "09-0000001". As this the primary key of the table it gives me error I have mentioned (Violation of Primary key, cannot insert duplicate value). When I changed Data Entry property = No, it fetched the last record and incremented it using the rest of the code.
Thanks MissingLinq, ChipR and Adezi for your precious inputs.
Dec 11 '09 #15
missinglinq
3,532 Recognized Expert Specialist
Glad you got it figured out and interesting tidbit of knowledge to know!

Linq ;0)>
Dec 11 '09 #16

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

Similar topics

3
4759
by: Ilan Sebba | last post by:
I have a 'supertype' table with only one field: autonumber. Call this table the 'parent' table. There are two subtypes, 'androids' and 'martians'. Martian have only one thing in common: they give...
33
4241
by: Lee C. | last post by:
I'm finding this to be extremely difficult to set up. I understand that Access won't manage the primary key and the cascade updates for a table. Fine. I tried changing the PK type to number and...
35
7239
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...
4
5149
by: yf | last post by:
A KB article "http://support.microsoft.com/default.aspx?scid=kb;en-us;209599" tells that the maximum number of records that a table may hold if the PRIMARY key data type is set to AUTONUMBER is...
26
3767
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...
11
4468
by: Alan Mailer | last post by:
A project I'm working on is going to use VB6 as a front end. The back end is going to be pre-existing MS Access 2002 database tables which already have records in them *but do not have any...
7
2477
by: CodeGunnerLev1 | last post by:
Good day guys... I must say this forum is quite of useful one. I really appreciate what you guys are doing and I hope it will continue on. I have few questions that needs some help. 1. I want...
2
1076
by: John Torres | last post by:
I’m trying to create a customized table for Purchase Order, where Purchase Order will be the Primary Key, 5 characters number, increments and autonumber and starts with 10000. How do I do that?...
6
11745
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
7223
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
7314
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,...
1
7030
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...
0
7482
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...
0
5623
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,...
1
5041
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...
0
1540
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 ...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.