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
2 2380
The easiest Method, in my mind, is to: - Manually enter ('seed), the 1st Case Number in your Table, say Table1, into the Field [CaseNum].
- Execute the following Function which will Increment the Number, then properly Format it.
-
Public Function fIncrementCaseNumber() As String
-
Dim bytYear As Byte
-
Dim strLastCaseNum As String
-
Dim bytCurrentYear As Byte
-
-
strLastCaseNum = DMax("[CaseNum]", "Table1")
-
-
bytYear = CByte(Left$(strLastCaseNum, 2)) 'Extract the Year
-
bytCurrentYear = Format$(Date, "yy") 'Current Year
-
-
If bytYear <> bytCurrentYear Then 'A Year change?
-
fIncrementCaseNumber = CStr(bytCurrentYear) & "-000001"
-
Else
-
'Increment and Format the Numeric Case Number Component
-
fIncrementCaseNumber = Left$(strLastCaseNum, 3) & Format$(Val(Right$(strLastCaseNum, 6) + 1), "000000")
-
End If
-
End Function
- 'Execute Function:
-
fIncrementCaseNumber()
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 : - Determining the code itself.
- Check the table as it stands.
- Find the largest value that matches the current year.
- If none exists yet, assume this to be zero (0).
- Increment this number.
- Format the code using the year value and this newly determined number.
- 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.
- 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 : - Private Sub Form_BeforeInsert(Cancel As Integer)
-
Dim strWhere As String
-
-
strWhere = "[CodeField] Like '" & Format(Date, "yy") & "-*'"
-
Me.CodeControl = Format(Date, "yy") & "-" & _
-
Format(Nz(DMax("Val(Right([CodeField],6))", _
-
"[Table]", _
-
strWhere), 0) + 1, "000000")
-
End Sub
Welcome to Bytes!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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.
|
by: vinay22 |
last post by:
how To Create Auto Generated Unique Identity Code in a SQL Table , when i Insert Data into TAble
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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,...
| |