Solved

Auto Increment Text Field

Posted on 2014-04-08
4
57 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 120

Expert Comment

by:Rey Obrero (Capricorn1)
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

813 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

16 Experts available now in Live!

Get 1:1 Help Now