Increment a Text String (from ABY to ABZ to AZA etc.)

Category:
String Manipulation
Type:
Snippets
Difficulty:
Beginning
Author:
Mike Zukerman

Version Compatibility: Visual Basic 6, Visual Basic 5

More information:
It is sometimes desirable to generate a string analogous to the autonumber, that is, to have a way of "incrementing" a text string. This might be useful if, for instance, you have a database field that (a) is textual, (b) has to be unique, and (c) has to be generated programmatically as you add a new record. The following function accepts a string and returns the one that "comes next", as is illustrated in the following table:

If you pass inThe function will return
ABCABD
ABYABZ
ABZACA
AZZBAA
ZZZAAAA

In other words, it will work just as you would expect it to for a numeric argument, incrementing the last position if possible or resetting it to A and incrementing the one to its left. The suggested function is case-insensitive; it will preserve the case used in the original string.

Sample use:

Text1.text = IncrementTextString(Text1.text)

Instructions: Copy the declarations and code below and paste directly into your VB project.

Declarations:

Code: