Increment AlphaNumeric Characters

1. Junior Member
Join Date
Dec 1969
Posts
24

## Increment AlphaNumeric Characters

I know I am not the first to run across this particular dilema so I am hoping someone has a snippet of code I can borrow.<BR><BR>I have a 3 character string that is, for example, B09. I need a bit of code to increment it to B10. If it reaches B99, I have to increment it to C00.<BR><BR>The string could also all numeric and when I hit 999, for example, I have to increment it to A00.<BR><BR>I understand that I will repeat after a certain point, but there are other values that are going to get concatenated to this later on.<BR><BR>Any help is appreciated!

2. Senior Member
Join Date
Dec 1969
Posts
11,247

## RE: Increment AlphaNumeric Characters

alpha = right(text, 1)<BR>numeric = mid(text, 2)<BR>select case numeric<BR> case 99<BR> alpha = chr(asc(alpha) +1)<BR> numeric = 00<BR> case else<BR> numeric = numeric + 1<BR>end select<BR>text = alpha & numeric

3. Senior Member
Join Date
Dec 1969
Posts
96,118

## Nope, that won't work

numeric = 00<BR>is no different than doing<BR> numeric = 0<BR><BR>You&#039;ll increment from (for example) C99 to D0 (only one zero).<BR><BR>On top of that, the code to get the alpha is wrong: RIGHT???<BR><BR>*************<BR><BR>If you *can* maintain a *separate* counter, then one easy way would be this:<BR><BR>&#060;%<BR>prefixes = "9ABCDEFGHIJKLMNOPQRSTUVWXYZ"<BR>Function counterToCode( counter )<BR> counterToCode = Mid( prefixes, counter100, 1 ) _<BR> & Right( "0" & counter, 2)<BR>End Function<BR>%&#062;<BR><BR>That will convert a counter to code thus:<BR> 0-99 --&#062;&#062; 900-999 <BR> 100-199 --&#062;&#062; A00-A99<BR> 200-200 --&#062;&#062; B00-B99<BR> etc.<BR><BR>Adjust the prefixes string to whatever you wish. (e.g., many places omit letters I and O to avoid confusion with numbers)<BR><BR><BR>

4. Senior Member
Join Date
Dec 1969
Posts
11,247

## </blush>

ooops<BR>And Yes Bill as usual you have the *better* idea

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## Fixing WK's idea...

If you don&#039;t need explicit control over the prefixes, this is better:<BR><BR>Function incrementCode( prior )<BR> Dim alpha, numeric<BR> alpha = Left(prior, 1) <BR> numeric = CInt( mid(prior, 2) )<BR> If numeric = 99 Then alpha = Chr( Asc(alpha) + 1 ) <BR> numeric = numeric + 1 &#039; always...99 goes to 100...so what?<BR> incrementCode = alpha & Right( "0" & numeric, 2 )<BR>End Function<BR><BR>

6. Senior Member
Join Date
Dec 1969
Posts
96,118

## Actually, yours is better...

...if you don&#039;t need to control the prefix more closely.<BR><BR>It just needed "tweaking".<BR><BR>

7. Senior Member
Join Date
Dec 1969
Posts
11,247

## after Z99 what happens

Wonder what you want when you get to Z99? restart at A or maybe AA?

8. Junior Member
Join Date
Dec 1969
Posts
24

## RE: Fixing WK's idea...

It has never been tested, but I am going to assume it goes from 000 to ZZZ and then go back to 000 if we use them all up.<BR><BR>I am trying to replicate in ASP a process that runs on a different system (Vax EMS).

9. Junior Member
Join Date
Dec 1969
Posts
24

## RE: Fixing WK's idea...

What do you mean by explicit control? I am going to grab a value from a column and increment it accordingly.

10. Senior Member
Join Date
Dec 1969
Posts
96,118

## See my ''nope'' message...

I meant that you could leave out letters I and O for example.<BR><BR>If you want this to go 0123456789ABC, etc., then WK&#039;s solution (and my fix) will *not* work as is.<BR><BR>You&#039;ll have to explicitly test for "9" as the "alpha" value and change it to "A". All other characters can just use the code as is, until/unless you decide what to do about Z99.<BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•