Solved

Auto Increment Text Field

Posted on 2014-04-08
4
54 Views
Last Modified: 2016-05-29
Hello,

I have a 2 character text field that I need to increment automatically in a table via VBA.  For example, I have a record with the pseudo key AA that I need to create a new record for under pseudo key AB.  I have thousands of records that I need to do this for.  Here are some examples of the keys I'm working with: AA, CB, AZ, ZA, PQ, et...the records that are created look at the alphabet for the second digit and adds the next consecutive letter.   How would I be able to do this via VBA?

Thank you.
0
Comment
Question by:axfernand
4 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 39987452
you need to use VBA codes to achieve this

test this function,

Function getNextValue(vStr As String)
Dim vChar As Long, firstChar As String
firstChar = Left(vStr, 1)
vChar = Asc(Right(vStr, 1))

getNextValue = firstChar & Chr(vChar + 1)
End Function

Open in new window

?getNextValue("AA")
AB
0
 
LVL 7

Accepted Solution

by:
COACHMAN99 earned 500 total points
ID: 39987587
this one goes a bit further: (increments to ZZ)

Function getNextValue(vStr As String) As String
  Dim vChar As Long, firstChar As String, secondChar As String
  firstChar = Left(vStr, 1)
  secondChar = IIf(Len(vStr) = 2, Right(vStr, 1), "")
  If Asc(secondChar) = 90 And Asc(firstChar) = 90 Then MsgBox "We have a ZZ problem!", vbCritical
  If Asc(secondChar) < 90 Then
    secondChar = Chr(Asc(Right(vStr, 1)) + 1)
  Else
    firstChar = Chr(Asc(firstChar) + 1)
    secondChar = Chr(65)
  End If
  getNextValue = firstChar & secondChar
End Function
0
 

Author Comment

by:axfernand
ID: 40002376
Hi Guys,

Haven't had a chance to apply your code but will do next week.  Thanks for the responses.
0

Featured Post

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.

Question has a verified solution.

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

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
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…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

895 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