Solved

Auto increment text field Access 2007

Posted on 2011-02-14
10
2,189 Views
Last Modified: 2013-11-27
I need code to auto increment a text field.  The field is alpha numeric.  The structure of my field is as follows:
MD-11-0001
MD-11-0002
MD-11-0003
and so on.

The first part is the office, the second part is the 2 digit year and the third is the number that I need to increment. I created a form with an add new record button.  what I need that button to do is create a new record and find the highest numbered control number and add 1.  Based on the above structure.  Also, if possible it would be nice if the coding could add the 2digit year without my intervention.  This is a multi user database, but the access is minimal.  3-4 users.

0
Comment
Question by:newtt13_newtt13
  • 5
  • 4
10 Comments
 
LVL 77

Expert Comment

by:peter57r
ID: 34889833
If the last number for 2011 was md-11-0543
what would be the first number for md-12-
0
 

Author Comment

by:newtt13_newtt13
ID: 34889950
0001 would be the new record for the next year.so md-12-0001
0
 
LVL 77

Accepted Solution

by:
peter57r earned 500 total points
ID: 34890639
My basic advice would be to use 3 fields for the different parts and that will life generallyeasier.
Especially since you Must enter the department to get anywhere.

However, based on what you asked, and assuming you have a textbox/combo where you enter or choose the dept then you can use a button click event procedure to get the next number.

You would need a function like the one below to be placed in a standard module and called from the button click event.  The code must be amended to use your own table and field names instead of Myxxxx.

Function GetNext(pDept As String)
Dim maxval
Dim prefix As String
prefix = pDept & "-" & Year(Date - 2000)

maxval = DMax("MyId", "Mytable", "left(MyId, 5) ='" & prefix & "'")
If IsNull(maxval) Then
     GetNext = prefix & "-0001"
Else
    GetNext = prefix & "-" & Format(Val(Right(maxval, 4)) + 1, "0000")
End If
End Function

Then I am assuming you have a textbox called txtDept on your form, a textbox called txtID, and a button which requests the next number.

So the button click event procedure would just have..

me.txtID = GetNext(me.txtDept)



0
 

Author Comment

by:newtt13_newtt13
ID: 34892014
Ok so I dont know a thing about VB.  What if I do not have a field called txtDept?  
0
 

Author Comment

by:newtt13_newtt13
ID: 34892060
Let me clarify that, the dept id field does not exist in my database.  The dept name just gets typed within my control number field that I am trying to auto increment.
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 77

Expert Comment

by:peter57r
ID: 34897351
You don't need a Dept field in the table but you DO need a separate textbox on the form where the user enters the dept so that it can be used by the code to find the next number.

If there is a known list of dept codes you should create a table of these and use a combobox instead of a textbox on the form to allow the user to select from the list.  That will ensure consistency and reduce errors.
0
 

Author Comment

by:newtt13_newtt13
ID: 34898701
peter57r,
you are a rockstar.  I finally got the event procedure to create a new record and auto input the control number.  However, the new record is coming up as MD-2005-0001.  Any idea why the date is coming up as 2005? Also how can I get that condensed to a 2 digit year?
0
 
LVL 77

Assisted Solution

by:peter57r
peter57r earned 500 total points
ID: 34898749
Sorry - a typo there...) in the wrong place

prefix = pDept & "-" & Year(Date) - 2000

0
 
LVL 31

Expert Comment

by:Helen_Feddema
ID: 34899649
For a 2-digit year string, use Format(Date(), "yy")
0
 

Author Closing Comment

by:newtt13_newtt13
ID: 34899665
Works great! Thanks
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now