Solved

Auto increment text field Access 2007

Posted on 2011-02-14
10
2,353 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 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

617 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