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

Generate primary key with actual date & increment number

Hi i have a field called "wosubID" which is the work order issued for subcontractors which is the primary key.

i want to have the code generated in the following format, mmyy-increment numbers.

In the form i've created i have created an event on this field which is as follows:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeInsert(Cancel As Integer)
  2.  
  3.     Me.wosubID = Format(Now(), "mmyy")
  4.  
  5. Exit_Here:
  6.     Exit Sub
  7.  
  8. End Sub
  9.  
how to i add the part for the numbers which should look something like

0316-001
0316-002
and so on

can anyone help
Mar 21 '16 #1
4 1338
PhilOfWalton
1,430 Expert 1GB
Try

Expand|Select|Wrap|Line Numbers
  1. Function FunnyFormat() As String
  2.  
  3.     Dim MyDb As Database
  4.     Dim Rst As Recordset
  5.     Dim Num As Integer
  6.     Dim LargestNum As Integer
  7.  
  8.     Set MyDb = CurrentDb
  9.     Set Rst = MyDb.OpenRecordset("SELECT WoSubIDs.* From WoSubIDs;")
  10.  
  11.     With Rst
  12.         Do Until .EOF
  13.             Num = CInt(right(!WoSubID, 3))
  14.             If Num > LargestNum Then
  15.                 Num = LargestNum
  16.             End If
  17.             .MoveNext
  18.         Loop
  19.         .Close
  20.         Set Rst = Nothing
  21.     End With
  22.  
  23.     LargestNum = LargestNum + 1
  24.    FunnyFormat = Format(Date, "mmyy") & "-" & Format(LargestNum, "000")
  25.  
  26. End Function
Phil
Mar 21 '16 #2
thanks Phil
the previous code i wrote was based on the event before insert in my form.
for the above code where should i use it?
Mar 21 '16 #3
PhilOfWalton
1,430 Expert 1GB
Sorry, had a bit wrong in the code. It should have been LargestNum = Num, not the other way round.
Anyway just use it in the BeforeInsert

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeInsert(Cancel As Integer)
  2.  
  3.     Dim MyDb As Database
  4.     Dim Rst As Recordset
  5.     Dim Num As Integer
  6.     Dim LargestNum As Integer
  7.  
  8.     Set MyDb = CurrentDb
  9.     Set Rst = MyDb.OpenRecordset("SELECT WoSubIDs.* From WoSubIDs;")
  10.  
  11.     With Rst
  12.         Do Until .EOF
  13.             Num = CInt(right(!WoSubID, 3))
  14.             If Num > LargestNum Then
  15.                  LargestNum = Num
  16.             End If
  17.             .MoveNext
  18.         Loop
  19.         .Close
  20.         Set Rst = Nothing
  21.     End With
  22.  
  23.     LargestNum = LargestNum + 1
  24.    WoSubID = Format(Date, "mmyy") & "-" & Format(LargestNum, "000")
  25.  
  26. End Sub
Mar 22 '16 #4
jforbes
1,107 Expert 1GB
Are you expecting to increment the numeric part indefinitely, or are you expecting to reset it every year back to 001?
Mar 22 '16 #5

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

Similar topics

3
by: Atreju | last post by:
I have the need to import logs into an Access table. The first field is a date & time field, represented in this example: 05/15/2004 17:58:55.336 This is how the logs are reported. DateTime....
13
by: Ron | last post by:
Hi all I'm deciding whether to use the PK also as an account number, invoice number, transaction number, etc that the user will see for the respective files. I understand that sometimes a...
6
by: Kd | last post by:
I have the OrderNo set as the following Right(Format(Date(),"yyyy"),1) & Format(Date(),"mm") & "-"Format(,"000")+1 This works great until I get to a new month and need the numbers to reset like...
3
by: divya | last post by:
Hi, I have a table tblbwday with 2 fields Name and Birthday.I have written this script for displaying evryday names of the people on that day. <% set objConn...
2
by: drurjen | last post by:
Good morning. I am importing an XLS file into one of my tables. The fields are: Date Id Time IO 12/22/2006 2 12:48:45 PM 9 12/22/2006 16 5:40:55 AM 1 12/22/2006 16 12:03:59 PM 2 ...
3
by: CourtGuy | last post by:
Hi Folks, I've got a problem that's been confounding me for months. I work for a criminal court, and I'm trying to set up an Access database to track petitions filed by criminal defendants. ...
1
by: adeebraza | last post by:
Hi, Every Body Following is code for Showing Actual Date & Time on the form and also record Date & Time of an event. See the following and use Call Modified when you want to record an event in...
9
by: beemomo | last post by:
Hi, I have a table name ExpenseClaim where it has a field named RefNo to store the Primary ID for each record. In my form, I has a combo box named cboExpenseCode: - 1) If user select "Company...
1
by: DANY IGHNATIOS | last post by:
hi. i want to update a row in an ACCESS DB in vb.net using sql query. the way to point to the row is using the primary key. the problem is that i was an error saying that there is a data type...
6
by: anguyen | last post by:
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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
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.