Auto Increment Invoice Number For Vb.net

12:10 AM
Let's say you have some invoice numbers which contains Alphabets as well as numeric number.
And you want to increment it one by one.

For Example:

if Invoice number is "AZ99999999" then Next Invoice Number will be "BA00000001"

Notice here that , the invoice number's lenght is 10 Characters out of which first 2 are Alphabets and the rest (8) are Numeric. invoice number can be of any digit with any combination of numerics and alphabets.

The function can be changed to your need very easily. but here i will demonstate for the above example.

Function:
Public Function IncrementInvoice(ByVal strInvoiceNumber As String) As String

        If strInvoiceNumber.Length <> 10 Then
            Return "Error"
        End If

        Dim strAlphaPart(1) As Char
        strAlphaPart(0) = strInvoiceNumber(0)
        strAlphaPart(1) = strInvoiceNumber(1)

        Dim IntPart As Int64
        IntPart = strInvoiceNumber.Substring(2, 8)


        If IntPart = 99999999 Then
            If strAlphaPart(1) = "Z" Then
                strAlphaPart(0) = Chr(Asc(strAlphaPart(0)) + 1)
                strAlphaPart(1) = "A"

                IntPart = 1

                Return strAlphaPart(0) & strAlphaPart(1) & IntPart.ToString.PadLeft(8, "0")
            Else
                strAlphaPart(1) = Chr(Asc(strAlphaPart(1)) + 1)
            End If

        Else
            IntPart += 1
            Return strAlphaPart(0) & strAlphaPart(1) & IntPart.ToString.PadLeft(8, "0")
        End If

    End Function



Output:
'outputs example:
strTemp = IncrementInvoice("AA99999998") 'Output will be: "AA99999999"
strTemp = IncrementInvoice("AA00000005") 'Output will be: "AA00000006"
strTemp = IncrementInvoice("AZ00000007") 'Output will be: "AZ00000008"
strTemp = IncrementInvoice("AZ99999999") 'Output will be: "BA00000001"



2 comments:

{ Abhi } at: November 18, 2011 at 8:21 PM said...

IT works!! thnx

{ krugm0f0 } at: May 6, 2020 at 12:39 PM said...

Is this usable in VB for my form in Excel? To add an increasing order number in ranges A0001-A9999 then B0001-B9999 etc.

Post a Comment