473,461 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Generate unique ID based on year, number reset each year

Hello everyone,

I want to generate a unique ID number for a test number field in the format NTN-YY-XXX where YY is the two digit of the year, and XXX is the sequential number. This number reset to 001 each year. For example, the first row generates this year would have the number NTN-12-001, next one would be NTN-12-002 ... Next year, the first number would be NTN-13-001. Would someone kindly show me how to achieve this?

I started with this: "NTN-" & Right$[DatePart("YYYY", NOW(),2)
but don't know how to but in the auto increment part and has the increment reset.

Thanks,
Al
Jan 30 '12 #1
6 7553
NeoPa
32,556 Expert Mod 16PB
In real terms what you do is to determine the current year, then look in the table to see the maximum value used so far that matches the format for the year. Make sure to treat the situation where no records are found as returning a valid string in the correct format. With the returned value simply take the number part and add one to it for the information required to build the new key with.

Assuming you have a table called [tblData], with a PK called [PK] (as you didn't share this information in the question unfortunately. Please make sure to include all relevant info on future questions.) then the code might look similar to :
Expand|Select|Wrap|Line Numbers
  1. Private Function NewPK() As String
  2.     Dim strTemplate As String
  3.  
  4.     strTemplate = Replace("NTN-XX-", "XX", Right(Year(Date()), 2))
  5.     NewPK = Nz(DMax(Expr:="Right([PK], 3)", _
  6.                     Domain:="[tblData]", _
  7.                     Criteria:="[PK] Like " & strTemplate & "*"), "0")
  8.     NewPK = strTemplate & Format(Val(NewPK) + 1, "000")
  9. End Sub
Jan 30 '12 #2
Yes, I should be more clear, thank you for your help!
Jan 31 '12 #3
I got a syntax error (missing operator) in query expression '[PK] Like NTN-12-*. I have the table [tblData] and [PK]. VBA is new for me, I apologize if I ask silly question; I mostly deal with Oracle.
Jan 31 '12 #4
NeoPa
32,556 Expert Mod 16PB
Not a silly question at all. I made a complete nooby error when I posted that code. While you read my updated version I'll just go out the back and beat myself with a stick for the embarrassment I just caused myself :-D
Expand|Select|Wrap|Line Numbers
  1. Private Function NewPK() As String
  2.     Dim strTemplate As String
  3.  
  4.     strTemplate = Replace("NTN-XX-", "XX", Right(Year(Date()), 2))
  5.     NewPK = Nz(DMax(Expr:="Right([PK], 3)", _
  6.                     Domain:="[tblData]", _
  7.                     Criteria:="[PK] Like '" & strTemplate & "*'"), "0")
  8.     NewPK = strTemplate & Format(Val(NewPK) + 1, "000")
  9. End Sub
Jan 31 '12 #5
Thanks, that did the trick with the debug editor, but I tried to enter the data from table, the PK did not populate automatically? Do I need to refer the function name from the PK to trigger it? I'm entering a different world of database.
Jan 31 '12 #6
NeoPa
32,556 Expert Mod 16PB
This question is a new one and doesn't relate to the original one (There is no information provided from which to answer you). I suggest you post a new question (You may link to this if you like.) where you include the code you have that calls this procedure (as that's what will determine populating the [PK] field).
Jan 31 '12 #7

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

Similar topics

2
by: Mullin | last post by:
I need to generate a unique no. in the format like yyyymmddxxxxxx (xxxxxx 6-digitl running number) I think of create a table with two columns date value 20050405 120 20050406 99...
10
by: Mamuninfo | last post by:
Hello, Have any function in the DB2 database that can generate unique id for each string like oracle, mysql,sybase,sqlserver database. In mysql:- select md5(concat_ws("Row name")) from...
1
by: hikums | last post by:
I am posting this here, just in case anyone may need this. Step 1: CREATE SEQUENCE ID_SEQ START WITH 1050000 INCREMENT BY 1 MAXVALUE 9999999 NO CYCLE NO CACHE ORDER
4
by: Ed | last post by:
I have a new requisite from a client that wants to give the user the option to about a databind based on number of returned rows. if ds.rows.count < 1000 then dg.databind() etc. They want...
28
by: Nina | last post by:
I need to generate unique id for my application. Use system time to create id, which is the best fit, but I don't know how to do it. Please help. Thanks in advance for any input.
6
by: Brian | last post by:
I hv a school assignment that is to generate 52 different random number, where all the numbers must >1 and <53. Pls help~ Thanks~
15
by: Ashish Khandelwal | last post by:
As MSDN is not giving us guarantee upon uniqueness of Hash Code, so could any one suggest me that how to generate a unique Hash Code for same string always, and generate different-2 Hash Code...
4
by: gaurav1983 | last post by:
i have to generate unique combinations of given number of digits entered by user eg: N=4 (0,1,2,3) output should be 0 1 2 3
1
by: situ | last post by:
Hi, I'm using DB2 V9 for windows I'm inserting records into DGTT from select statement; the problem is how to insert unique value for every row inserted. I tried using “generated always as”...
6
by: er | last post by:
hi, here's why i'm trying to do: header1.hpp namespace{ struct A{};} struct B1{ A a; }; header2.hpp namespace{ struct A{};}
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
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...
0
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,...
0
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
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.