Auto Increment Invoice Number For Vb.net

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 length 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 numeric and alphabets.

The function can be changed to your need very easily. but here i will demonstrate 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”

 

Sandeep

a dev, an amateur photographer and a father

 

One thought on “Auto Increment Invoice Number For Vb.net

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: