IncrTextString Function:
Increment a Text String (from ABY to ABZ to AZA etc.)
Description:
It is sometimes desirable to generate a string analog to the autonumber, that is, 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 in The function will return ABC ABD ABY ABZ ABZ ACA AZZ BAA ZZZ AAAA
Syntax:
string = IncrTextString(string)
Example:
<%
Response.Write IncrTextString("ZZ") '--- returns: AAA
%>
ASP Source Code:
<%
Function IncrTextString(ByVal strText) Dim L, i, c Dim S S = strText L = Len(S) For i = L To 1 Step - 1 c = Asc(Mid(S, i, 1)) If (c >= 65 and c <= 89) Or (c >= 97 And c <= 121) Then S = Left(S, i - 1) & Chr(c + 1) & Mid(S, i + 1) Exit For ElseIf c = 90 Then S = Left(S, i - 1) & "A" & Mid(S, i + 1) ElseIf c = 122 Then S = Left(S, i - 1) & "a" & Mid(S, i + 1) End if Next If i = 0 Then IncrTextString = String(L + 1, 65) Else IncrTextString = S End If End Function
%>
|