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

Custom Primary ID ( not Autonumbers)

Dear All,
I know that this question was asked many times but I am new to access and programming field.
I have created a tracking DB where incidents/accidents are tracked by Event ID number for which I have linked a table called recommendations RecID.
What I need is following:
1. The EventID shall be this format: Dept-EVENT#-Year.
2. The Recommendation ID shall be Dept-Event#-Year-Rec#

With the new year change the Event# shall reset to 1 and for every EventID the recommendation ID shall start with 1
example: ASD-0123-2011 ( EVENTID)
ASD-0123-2011-01 (RECID)
ASD-0123-2011-02 (RECID)
ASD-0123-2011-03 (RECID)

Anyone can help me please. Its quit urgent and if dont implement this I might loose my job.
May 18 '11 #1
3 2840
NeoPa
32,556 Expert Mod 16PB
It's certainly unusual (and probably counter-intuitive even) to have the sequence number to the left of a static value like the year, but it makes little difference fundamentally. It may just make understanding the logic a little more difficult for someone new to it.

Anyway, what you have to do is find the maximum matching value currently in the table, extract the number value from that, increment the number determined and then create a new one using that number.

I'm assuming from what you say that all this data is being stored in the same table (which would be a thoroughly bad idea but I have no other information so must assume that for now).

Assuming then, that we have variables indicating the Dept and for working with the Event# & Rec# we might have something like :

Expand|Select|Wrap|Line Numbers
  1. Dim strDept as String, strEventID As String, strRecID As String
  2. Dim lngEvent As Long, lngRec As Long
  3.  
  4. strDept = "ASD"
  5. strEventID = strDept & "-*-" & Year(Date)
  6. lngEvent = Val(Split(Nz(DMax("[EventID]", _
  7.                              "[Recommendations RecID]", _
  8.                              strEventID), "-0-"), "-")(1)) + 1
  9. strEventID = Replace(strEventID, "*", Format(lngEvent, "0000"))
  10.  
  11. strRecID = strEventID & "-*"
  12. lngRec = Val(Split(Nz(DMax("[RecID]", _
  13.                            "[Recommendations RecID]", _
  14.                            strRecID), "---0"), "-")(3)) + 1
  15. strRecID = Replace(strRecID, "*", Format(lngRec, "00"))
PS. It's generally a good idea, when you're in a hurry, to make sure you provide all the information likely to be required, otherwise the extra time taken (and all the experts who turn away from your question) are eating into your precious time.
May 18 '11 #2
Thanks! NeoPa, for your precious time and efforts you hav put in this code.
I just want to eloberate little more:
I have two forms The parent form control source is EventTbl and child form is RecommedTbl.
I just want to ask you where do I put this code in to use it and the second is I want in the first form only the Year-ASD-0001 and the recommedID shall be in sub form such as Year-ASD-0001-01,02,03....
Kindly help me out in implenting this.
Thanks!
May 20 '11 #3
NeoPa
32,556 Expert Mod 16PB
I'm afraid I can't really help there.

Generally we don't like to do the work for you, which is why I have posted code which illustrates what you need to do, but which is no good to simply paste in. You need to take the code and put it where it makes sense within your project.

The other reason I can't help is that, even if I wanted to, I still don't have anywhere near enough information about your project to do it.

All that said, if you know your own project (database) it should be a simple matter to amend this code to fit it. It's a pretty close match already. Only you can do that though, as only you have the database.
May 21 '11 #4

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

Similar topics

7
by: Ilan Sebba | last post by:
I am trying to add a record using SQL. My problem is that the primary keys are foreign keys, and these foreign keys are autonumbers. I therefore do not know the primary keys of the record I am...
4
by: Megan | last post by:
Hi- I need some help/ advise on how to code unique numbers for the primary keys of my 2 tables. I inherited a database that covers information about Hearings and Rulings. Information about the...
2
by: Megan | last post by:
Hi everybody- I have 2 tables, Hearings and Rulings. Both have primary keys called, CaseID, that are autonumbers. I don't want both tables to have the same autonumber. For example, if Hearings...
7
by: sea | last post by:
Is it a good idea to programatically create a primary key? For example in a table called names, I have the following fields, (1) firstname (2)lastname (3) ID - will it be ok to create a primary...
13
by: Ron | last post by:
Hi All, Okay, I've read previous suggestions about not showing the primary key to the user of forms. If a number is to be shown (let's say, customer number) it should be generated independently...
1
by: GGerard | last post by:
Hello I'm trying to find the best way to set indexes and primary keys on MSAccess tables What are the advantages and disadvantages of indexes and primary keys? What fields should be indexed?...
115
by: LurfysMa | last post by:
Most of the reference books recommend autonum primary keys, but the Access help says that any unique keys will work. What are the tradeoffs? I have several tables that have unique fields. Can...
8
by: Imicola | last post by:
Hi, I want to use primary key IDs in my tables which are autonumbers, with a text prefix. I.e. for table tblVillage, the ID's will by V1, V2, V3... and for table tblHumanCases, the ID's will be...
3
by: AMD_GAMER | last post by:
I currently have a website which is using ASP.NET 2.0, C#, and SQL Server 2005. The website will be used to enter grants for a university. When a new grant is entered, I need to generate a primary...
5
by: PotatoChip | last post by:
I'm trying to work out how to format a custom primary key using the current year as part of the key. I've copied code which I use 'OnCurrent' for the form. The code I've copied is: Private Sub...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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
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.