473,662 Members | 2,724 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 2796
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_BeforeUpda te, 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 _RadioCallActiv ity cannont insert duplicate key in object 'dbo.RadioLog_t blRadioCallActi vity. (#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

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

Similar topics

3
4766
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 birth to identical mules. So each android and a martian have primary key which is a foreign key to the parent table. Now, I want to insert a new record in 'android' or 'martian'. This can be done easily using MS-Access forms. But I want to do...
33
4280
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 setting default value to a UDF that manages the auto-numbering. Access won't take a UDF as a default value. Okay, I'll use SQL WITHOUT any aggregate functions, for the default value. Access won't do that either. Okay, I create a second...
35
7259
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 start at 51 after the last 50 records are deleted? Thanks! Traci
4
5162
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 4,294,967,295. Suppose the PRIMARY key data type is set to "RANDOM" AutoNumber. Suppose an application (a) successfully INSERTS "X" records, then (b) successfully DELETES "Y" records (X >= Y), then
26
3803
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 assigned to a record that it doesn't change. Occasionally I find that due to corruption or an accidental deletion and restore of a record from a backup the autonumber field needs to be tidied up. So when I create (through AddNew) the autonumber...
11
4485
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 AutoNumber* fields in them. Correct me if I'm wrong, but I'm assuming this means that I cannot now alter these existing Access tables and change their primary key to an "AutoNumber" type. If I'm right about this, I need some suggestions as to the...
7
2492
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 to know how do you create a primary key that increment itself but with a specified format. Say s3075711 something like that. I doesn't have to be like that but similar to it. And for distributed database, is it possible to say, give a certain...
2
1081
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? Thanks John Vista Access 2000
6
11756
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 contents of the form is inserted into the MS Access database. The Customer table in the database already has 30 records (with CustomerIDs 1 - 30) in it (from when the database was first created). The CustomerID field in the database is an...
0
8435
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8345
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8857
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8547
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 most users, this new feature is actually very convenient. If you want to control the update process,...
1
6186
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5655
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4181
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2763
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
2
1999
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.