Solved

Auto increment text field Access 2007

Posted on 2011-02-14
10
2,212 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sort Time by AM and PM in query 2 21
Run Time Error 3075 15 47
Query design issue 2 24
Export to flat file from Access 2003, not using saved spec 9 3
Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

864 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

19 Experts available now in Live!

Get 1:1 Help Now