Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Auto increment text field Access 2007

Posted on 2011-02-14
10
Medium Priority
?
2,385 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
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.

 

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
 
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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

722 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