generate alphanumeric codes

which is the best way to generate all the alphanumeric combinations of a 3 characters field ?

I would like to get this result:

A01
A02
A03
.
.
B01
B02
.
.
AA1
AA2
.
.
AB1
AB2
.
.
AAA

etc.

Thanks in advances

Luciano

In your example, what’s in the field that would generate those combinations?

[code] dim i,j,k as integer
dim IIS,JJS,KKS, ThreeDigit as String

for i=1 to 26
IIS=chr(i+64)

for j=0 to 36
  select case j
    
  case 0,1,2,3,4,5,6,7,8,9
    JJS=str(j)
    
  else
    JJS=chr(j+55)
    
  end select
  
  for k=0 to 36
    
    select case k
      
    case 0,1,2,3,4,5,6,7,8,9
      KKS=str(k)
      
    else
      KKS=chr(k+55)
      
    end select
    
    ThreeDigit = IIS+JJS+KKS
    
    //whatever you want to do with it ?
    
  next
next

next[/code]

Hi Kem and Robert,
sorry for my bed english, I’ll try to explain better.
I need to generate these alphanumeric codes (must be unique ,max lengh 3 and all characters must be uppercase) in order to address categories of products contained in a database, uniquely.
The function must return me this code (starting from A01) and keep trace of all codes already generated.
So, the first time that I’ll call the function, it will returning “A01”, than the second time “A02” and so on and after that the function will return the last “A99” it will returning the next time “B01” etc. . But every time must be a new code. (I need all possible combinations)

I’ve forgot to tell that everytime I’ll call the function I’ll pass the last generated code from the function itself

Thanks Robert,
it works fine (I’ve add an array where to save all 35594 possible combinations generated !! :slight_smile:

This scheme would be limited to 2,600 codes. Something like this should do it:

Function GenerateCode (lastCode As String) As String
  static alphabet as string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

  //
  // Do some error checking
  //
  if lastCode.Len <> 3 then
    // Deal with it
  end if

  dim firstLetter as string = lastCode.Left( 1 )
  dim firstIndex as integer = alphabet.InStr( firstLetter )
  if firstIndex = 0 then
    // Deal with it
  end if

  dim lastPart as integer = val( lastCode.Right( 2 ) )
  if lastPart = 0 then
    // Deal with it
  end if

  lastPart = lastPart + 1
  if lastPart > 99 then
    lastPart = 1
    firstIndex = firstIndex + 1
    firstLetter = alphabet.Mid( firstIndex, 1 )
  end if

  dim nextCode as string = firstLetter + format( lastPart, "00" )
  return nextCode
End Function

Note: I haven’t tested this.

Thats even more simple:

[code]Function GenerateCode (Oldstring As String) As String
dim A,B,C as string

A=left(oldstring,1)
B=mid(oldstring,2,1)
C=Right(oldstring,1)

if C=“Z” then
C=“0”
B=chr(asc(B)+1)
if asc(B)=58 then B=“A” // Now B is higher than “9”

if asc(B)>90 then  // 90 was Z and now its 91 !
  
  B="0"
  A= chr(asc(A)+1)
  
end if

else
if C=“9” then
C=“A”
else
C=chr(asc©+1)
end if
end if

If A+B+C = “ZZZ” then
MSGBOX “We are at the end”
end if

Return(A+B+C)

End function[/code]

Note I have tested it :wink:

But in the first Posting you wanted alphanumeric values at the last two digits and then you changed at A99 to B01 ??