Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tricky Loop - Help

Status
Not open for further replies.

monrosal

Programmer
Aug 22, 2000
42
0
0
US
Hi,

I am trying to do a loop in which I start with 4 alpha characters like "AAAA" and say I want to loop until "BBBB". The way I want it to loop is "AAAB, AAAC, AAAD, AAAE,.....AAAZ, AABA, AABB,....AABZ, AACA" all the way to BBBB. Does anybody know how I can do this? Thanks in advance

Ramon
 
Hi Ramon!

I'm not sure why you want to do this but here you go:

Dim strAlpha(1 To 26) As String
Dim intOuterLoop As Integer
Dim intSecondLoop As Integer
Dim intThirdLoop As Integer
Dim intInnerLoop As Integer
Dim strTester As String

strAlpha(1) = "A" etc (You can do this in the declaration)

For intOuterLoop= 1 To 26
For intSecondLoop = 1 To 26
For intThirdLoop = 1 To 26
For intInnerLoop = 1 To 26
strTester = strAlpha(intOuterLoop) & strAlpha(intSecondLoop) & strAlpha(intThirdLoop) & strAlpha(intInnerLoop)
Do the rest of your stuff here
Next intInnerLoop
Next intThirdLoop
Next intSecondLoop
Next intOuterLoop

Of course you can use the Char function instead or the strAlpha, but I can never remember the ASCII offset for different characters.

hth
Jeff Bridgham
bridgham@purdue.edu
 
I'm also quite puzzled by the need for such a function. Unlike Jeff, I am reluctant to jump into the fray w/o some background. I looked (briefly) into the permutations thing and decided that the average process - given your parameters would run out of some resource before completing the task. In my case, it could be operator patience. In others, is might be memory. I calculate that you would end up generating ~~1/2 MILLION strings, just getting from "A" to "B". To generate this pile of alpha - and not put it to use seems entirely ridiculous, yet you never mention what it is for. To use it, you would either need to store the various combos - or use them in-process in some manner. Further, in any pratical use I can envision, you would need to at least be able to vary the parameters in some manner (what "string" to start with and what condition(s) to terminiate the process). As soon as these elements are in place, the number of results become (rather wildly) variable. So, while I can see HOW to do this, I remain unconvinced that it is useful for me to do it.

MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
I would rather not use it myself because I thought it was pretty dumb too, but see the customer I'm working for has used this system for years, which is a unique identifier with Alpha characters. The customer matches the id with id from a hard copy documentation. A lot of people will only use something new if it closely resembles their existing system. People are scared of change.

Ramon
 
So, you are not storing the results, but doing an "Alpha-Incremet". i.e. you couls send the 'previous' (or 'highest') value from a field and expect the return to be the 'incremented' string. For example, you could send "AABC" and accept "AABD" as the return value? If so it is not all that hard. There are some previous posts which would easily do this, although many/most are perhaps slightly more elaborate than you need (include processig of punctuation, mixed case, and numeric characters within the string), but still not as complex as posted here.


While I would strongly reccomend that you 'convince (or coerce?) your customer to graduate to (at least the 20th Century), The routine to increment a string is not so very difficult.

One of my earlier efforts in this arena:

Code:
Public Function basIncrAlphN(strAlphN As String) As String

    'Michael Red.   12/21/2002
    'To Create an Incrementing A-N field with rollover
    'AAA01 to AAA99
    'AAB01 to AAB99

    Dim Idx As Integer
    Dim MyChr As String * 1
    Dim MyValStr As String
    Dim MyStrStr As String
    Dim MyValVal As Long

    'Easy on me, make user supply the last value.  "I" don't know where it is

    For Idx = 1 To Len(strAlphN)
        MyChr = Mid(strAlphN, Idx, 1)
        If (IsNumeric(MyChr)) Then
            MyValStr = MyValStr & MyChr
         Else
            MyStrStr = MyStrStr & MyChr
        End If
    Next Idx

    MyValVal = CLng(MyValStr) + 1
    If (MyValVal = 100) Then
        'Do The Rollover
        MyValVal = 1

        'Incr the String Part
        'Just put each char into Seperate Char
        MyStrStr = basIncrStr(MyStrStr)

    End If
    
    basIncrAlphN = MyStrStr & right("00" & Trim(str(MyValVal)), 2)

End Function

MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top