Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Auto increment text field Access 2007

Posted on 2011-02-14
10
Medium Priority
?
2,431 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 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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

Industry Leaders: 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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

916 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