473,399 Members | 2,159 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,399 software developers and data experts.

I need to create a computer generated case number

I am creating a database in Access 2002. I need to create a computer generated case number that starts with the year and is in the form YY-nnnnnn (6 digit number) that increments by 1. I am not a programmer but have some experience creating small databases. I am hoping that someone can assist me. Thank you
Sep 8 '10 #1
2 2380
ADezii
8,834 Expert 8TB
The easiest Method, in my mind, is to:
  1. Manually enter ('seed), the 1st Case Number in your Table, say Table1, into the Field [CaseNum].
  2. Execute the following Function which will Increment the Number, then properly Format it.
    Expand|Select|Wrap|Line Numbers
    1. Public Function fIncrementCaseNumber() As String
    2. Dim bytYear As Byte
    3. Dim strLastCaseNum As String
    4. Dim bytCurrentYear As Byte
    5.  
    6. strLastCaseNum = DMax("[CaseNum]", "Table1")
    7.  
    8. bytYear = CByte(Left$(strLastCaseNum, 2))   'Extract the Year
    9. bytCurrentYear = Format$(Date, "yy")        'Current Year
    10.  
    11. If bytYear <> bytCurrentYear Then           'A Year change?
    12.   fIncrementCaseNumber = CStr(bytCurrentYear) & "-000001"
    13. Else
    14.   'Increment and Format the Numeric Case Number Component
    15.   fIncrementCaseNumber = Left$(strLastCaseNum, 3) & Format$(Val(Right$(strLastCaseNum, 6) + 1), "000000")
    16. End If
    17. End Function
    Expand|Select|Wrap|Line Numbers
    1. First entry is 10-000001
    Expand|Select|Wrap|Line Numbers
    1. 'Execute Function:
    2. fIncrementCaseNumber()
    Expand|Select|Wrap|Line Numbers
    1. 'OUTPUT:
    2. 10-000002
Sep 8 '10 #2
NeoPa
32,556 Expert Mod 16PB
This question, in one form or another, is asked quite regularly on here.

There are a number of issues to consider :
  1. Determining the code itself.
    1. Check the table as it stands.
    2. Find the largest value that matches the current year.
    3. If none exists yet, assume this to be zero (0).
    4. Increment this number.
    5. Format the code using the year value and this newly determined number.

  2. Where (when) to determine the code.

    This should be done in the Form_BeforeInsert() event procedure. It can only make sense if it is applied immediately it is determined.

  3. When to display the code (related to 2 above).

    Not when entering the data for the record. As this cannot be known until the point the data is actually saved away (See point #2 above) then it would be invalid to display anything here until it has been. This is synonymous with an AutoNumber field, which is not shown until the record has been created.

Having discussed the concepts we can look at the code. I've tried to make it as clear as possible what each step is doing, but you'll have to put your own control, field and table names in yourself :
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeInsert(Cancel As Integer)
  2.     Dim strWhere As String
  3.  
  4.     strWhere = "[CodeField] Like '" & Format(Date, "yy") & "-*'"
  5.     Me.CodeControl = Format(Date, "yy") & "-" & _
  6.                      Format(Nz(DMax("Val(Right([CodeField],6))", _
  7.                                     "[Table]", _
  8.                                     strWhere), 0) + 1, "000000")
  9. End Sub
Welcome to Bytes!
Sep 9 '10 #3

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

Similar topics

5
by: Bura Tino | last post by:
Sorry if this is offtopic. I'm developing a website which, among other things, has contacts. I would like my users to be able to click on a number and have the computer dial that number. Is this...
1
by: Prince | last post by:
Hi all, Can anyone tell me how to create auto generated values in a DataTable column? Looking forward for the reply.... Thanx in advance...
0
by: Hani Atassi | last post by:
I am developing a secured Web Service that only specific Windows role can access it. I have the following in the configuration file: <authentication mode="Windows" /> <identity...
9
by: Nooby | last post by:
New to Access here. I inherited a db that has the first column as an automatically generated sequential number. I want to bump it up, but for the life of me I can't figure out how to do it. Is...
1
by: chance | last post by:
I need to write some c# code to get the last automatically generated sequence number from an Oracle database. Can anyone provide an example. tia, chance.
1
by: vinay22 | last post by:
how To Create Auto Generated Unique Identity Code in a SQL Table , when i Insert Data into TAble
16
by: malteselemon | last post by:
Using Access 2003, I need to create an automatic Serial Number. Example "7235-E001" "7" is current year "235" is day "-E" is static "001" is sequencial, restarting at "001" each day I set a...
8
by: Elfae | last post by:
I have searched high and low for a sample for this, and I just can't find any. Sorry for the length! Background Information The issue revolves around setting up a system-generated increase in...
0
by: BT08 | last post by:
I am creating an automated questionnaire (form) in excel to be used to entry data. I need to create a unique survey number. The survey number should be linked to a retrieve button. The goal is if...
0
code green
by: code green | last post by:
Anybody know how to SELECT a running total of the records being returned from a query when the line number is not provided. I am downloading product lines belonging to an order from a table using...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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
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,...
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
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
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,...

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.