Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Good day experts!

i'm trying to make a recursive alphabet for my ORDER NUMBER when order number reaches 500 (but in my test i set it to 1).The code below are working fine from A - Z upto AA,but when it comes to AB,it display AA and reset the ornumber to one which is right. What is wrong with my code?


VB
Private Sub generateORNumber()
        Dim con As New SqlConnection(ConnectString())
        Dim cd As New SqlCommand
        Dim lrd As SqlDataReader
        Try
            con.Open()
            cd.Connection = con
            cd.CommandText = "SELECT count(ornumber) as co,AlphabetID FROM BlueRoomTransactions group by AlphabetID"
            lrd = cd.ExecuteReader()
            While lrd.Read()
                ornum = Convert.ToString(lrd("co"))
                alpID = Convert.ToString(lrd("AlphabetID"))
            End While
        Catch ex As Exception
        Finally
            con.Close()
        End Try
        ornum = ornum + 1
        totalORNUM2 = ornum - 1

        txtORNumber.Text = "OR" & Date.Today.ToString("MMddyy") & alphRecursive().ToString & ornum.ToString.PadLeft(4, "0")


    End Sub


    Private Function alphRecursive() As String
        Dim alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray()

        If ornum > 1 Then
            alpID += 1
            c = alpID
            ''Reset ornum to zero(0) and add another alphabet on the existing string like 'AA,AB' after it reaches the designated ornum like 1
            If c >= alphabet.Length Then
                ornum = ornum - totalORNUM2
                c = 0
                Return alphRecursive(c \ alphabet.Length) & alphabet(c Mod alphabet.Length)
            Else
                ornum = ornum - totalORNUM2
                Return "" & alphabet(c Mod alphabet.Length)
            End If
        Else

            Return "" & alphabet(c Mod alphabet.Length)

        End If


    End Function
Posted
Comments
Wombaticus 19-Jun-14 12:24pm    
Not sure what's wrng with your code, but a simpler function would be someting like

Function fBase26Encode(ByRef lngNumToConvert As Long) As String
Dim s As New StringBuilder
Dim abc As Char() = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray
If lngNumToConvert > 0 Then
Do While lngNumToConvert <> 0
s.Append(abc(lngNumToConvert Mod 26))
lngNumToConvert = lngNumToConvert \ 26
Loop
End If
Return StrReverse(s.ToString)
End Function
Chester Costa 19-Jun-14 21:30pm    
thanks for your reply sir..but may i ask about the do loop?

this is the code you gave me:
this is where i put the the string:
txtORNumber.Text = "OR" & Date.Today.ToString("MMddyy") & fBase26Encode(ornum).ToString & ornum.ToString.PadLeft(4, "0")

"where ornum comes from the total count of data in my table which is in my case,when it reach two data in my table for example then next to my date string will add "A" and so on

Function fBase26Encode(ByRef lngNumToConvert As Long) As String
Dim s As New StringBuilder
Dim abc As Char() = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray
If lngNumToConvert > 2 Then
Do While lngNumToConvert <> 0
s.Append(abc(lngNumToConvert Mod 26))
lngNumToConvert = lngNumToConvert \ 26
Loop
End If
Return StrReverse(s.ToString)
End Function
Wombaticus 20-Jun-14 4:04am    
Sorry - change the ByRef in the function definition to ByVal - as it is, your ornum value is being set to 0 on each call

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900