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

Format autonumber field properties to include 2 digit year

1
I am attempting to set the field properties for the autonumber data type to yield an auto number that starts with a two digit year. The 4 digit year will also work. I am trying to get a result that looks something like this:

15-000001
15-000002
15-000003
etc...

or,

2015-000001
2015-000002
2015-000003
etc...

Thanks for your time and I appreciate any help you can give me.
Jan 14 '15 #1
4 7087
jforbes
1,107 Expert 1GB
Hello JDub22,

Here are a couple (three) Questions/Answers that are similar to the one you are asking, they might have what you need:
http://bytes.com/topic/access/answer...mber-data-type
http://bytes.com/topic/access/answer...ate-autonumber
http://bytes.com/topic/access/answer...eric-id-number

You could also use an AutoNumber column along with a Date Field that Defaults to the Current Date when the Record is Created. Then calculate the field in the format you want when you need to view it or print it.
Jan 14 '15 #2
I spent three days searching the internet for a solution to my problem. I had some help from our IT expert and I now have a solution. First, let me repeat what I needed. I needed a Condition Report(CR) Number which will drive my database. I created a field "CR Number". I needed information in that field to be prefixed with the current year, by last two digits, a hyphen, and a sequencing number that reverts back to 0001 at the beginning of each year. For example, I needed a number like: 15-0032 and the first CR of next year would be 16-0001.

There are solutions shown on this site, but not a lot of directions for someone like me with little to no coding or Access experience so I am going to detail not only the solution, but "how to" employ it in MS Access 2010.

Not only will this solution provide you with a sequential number and a two-digit year, but the macro below also restarts the sequence when the new year begins.

Directions:

I created a table (CR Database) with a field "CR Number" (data field: text). In the "Datasheet View", I clicked on the field/column for "CR Number". I then went to the ribbon under "Table Tools" and clicked on the ribbon "Table". Icons appeared on that ribbon and I clicked on the icon "Before Change". This is MS Access' Macro developer. Then I worked through the helping screens to insert the following code:



Expand|Select|Wrap|Line Numbers

Expand|Select|Wrap|Line Numbers
  1. 1.   If [IsInsert] Then
  2. 2.    SetLocalVar
  3. 3.       Name YearPrefix
  4. 4.       Expression = Format(Date(), "yy")
  5. 5. 
  6. 6.    SetLocalVar
  7. 7.       Name nextSeq
  8. 8.       Expression = 1
  9. 9. 
  10. 10.    Look Up A Record In  
  11. 11.       SELECT z.[CR Number] FROM [CR Database] AS z ORDER BY z.[CR Number] DESC;
  12. 12.       Where Condition = [z].[CR Number] Like [YearPrefix] & "-*"
  13. 13.       Alias z
  14. 14. 
  15. 15.    SetLocalVar
  16. 16.       Name nextSeq
  17. 17.       Expression = Val(Right([z].[CR Number],4)) +1
  18. 18. 
  19. 19.    SetField
  20. 20.       Name [CR Number]
  21. 21.       Value = [YearPrefix] & "-" & Format([nextSeq], "0000")
  22. 22.    End If
  23.  
When the screens pop up and you fill in the lines, some of what you are typing (IsInsert, SetLocalVar, Look Up A Record In, SetField, etc.) automatically populates or appears in drop down menus.

I hope this helps in this situation.
May 4 '15 #3
zmbd
5,501 Expert Mod 4TB
Just to clarify, SlingerJM appears to be using a table level event "data macro" that was introduced in Access 2010 which behave along the lines of "triggers" in the server type databases.

Keep in mind that that, to the best of my knowledge, you can NOT upsize an Access database that uses "data macro" in the table design.

For others, if VBA or SQL methods covered herein are giving you problems, feel free to start a new thread and either post your code and/or reference the thread/post here at bytes that is giving you problems - in doing so, you are helping others too! :)
May 4 '15 #4
NeoPa
32,556 Expert Mod 16PB
I tend to use DMax() with a Criteria that matches the date part of the value and an Expr that returns the numeric part. The returned value should be converted from a string using something like Val() and a lack of any entries found should be handled by using Nz(..., "0").

So, in your case something like :
Expand|Select|Wrap|Line Numbers
  1. Public Function NewKeyVal(strTable As String) As String
  2.     Dim strWhere As String
  3.  
  4.     NewKeyVal = Format(Date(), "yyyy\-")
  5.     strWhere = Replace("([Index] Like '%Y*')", "%Y", NewKeyVal)
  6.     NewKeyVal = NewKeyVal & Format(Val(Nz(DMax(Expr:="Mid([Index],6,6)" _
  7.                                              , Domain:=strTable _
  8.                                              , Criteria:=strWhere) _
  9.                                         , "0")) + 1, "000000")
  10. End Function
May 6 '15 #5

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

Similar topics

2
by: John Baker | last post by:
Hi: I need to set the standard for our Approach application that dates are always displayed with a 4 digit year. I cant find the place in Approach where this can be set, since all the formats...
5
by: MHenry | last post by:
Hi, I don't know what happened, but all the MS Access dates in all tables, queries, forms, and reports in all databases suddenly show on my computer with the year as 4 digits (including network...
1
by: S. van Beek | last post by:
Dear reader, By append a new record to a table I always expect the next higher value in the range of the autonumber field. But sometimes if I have deleted same records from a table the...
18
by: Dixie | last post by:
Can I set the Format property in a date/time field in code? Can I set the Input Mask in a date/time field in code? Can I set the Format of a Yes/No field to Checkbox in code? I am working on...
6
by: HS1 | last post by:
Hello I have a table in Access Database. This table has a AutoNumber field. I use a DataGrid to show that table When I insert a new record in for this table using a DataGrid, there is a...
0
by: Panayotis Kouvarakis | last post by:
I have noticed that CompareValidator and RangeValidator don't work properly with 2-digit year dates when using client script validation. Specifically the problem lies in the way the string is...
4
by: Amy Badgett | last post by:
I have figured out that there is no easy way to create an autonumber field in a query, but that there is a way (supposedly) to create an autonumber field in a table or rather, there is already an...
3
by: Katie Howard | last post by:
Hi, I’m desperately looking for some help… I maintain a Contacts database which has about 27,000 records. It was initially set up for the field Contact_ID to be an autonumber (also my PK), and...
3
by: jac1 | last post by:
I have seen a number of responses to other questions that indicate I should be able to do the following to insert a new record into an Access 2007 database and have my primary key autonumber field...
7
Rebecca Ross
by: Rebecca Ross | last post by:
I would like to reset an autonumber field to begin at 1 again for 2013, is there code that I could insert that would look at the number year field to accomplish this? Thanks...
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: 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: 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
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.