Wednesday, September 19, 2012

Fransiskus Sutris

Flexible AutoNumber In VB.NET (English)


Flexible Auto number

By using this function you able to make the flexible auto number, why called flexible? Because my function not only generate from number only, but will generate the auto number contained the letter and number. Such as invoice number, purchase order number, etc.

See illustrate on the table below:
Recent Number
Next Number
1
2
3
And so on
00001
00002
00003
And so on
PO-0001
PO-0002
PO-0004
And so on
INV-20.09.2012-00001
INV-20.09.12-00002
INV-20.09.2012-00003
And so on

Let's look at an example of using this function is simply to be easily understood.
1.
Create new project on your VB Express/.NET. Give Name Of Your Project with FlexibleAutoNumber.

2.
Put two labels, two textbox and one button (can be arranged on the picture below).
3.
Add a class in your project and name it with the name clsFunction.vb, see picture below.
4.
Open clsFunction.vb by double-click, then remove the two lines of code there, and paste the following code:


Public Class clsFunction
    Function GetNextNumber(ByVal strBefore As String) As String
        Dim temp1 As String = vbNullString
        Dim lnStrBefore, lnTemp1 As Integer

        temp1 = Val(strBefore) + 1

       lnStrBefore = Len(strBefore)
        lnTemp1 = Len(temp1)

        If lnStrBefore > lnTemp1 Then
            Dim a As Integer
            For a = lnTemp1 To (lnStrBefore - 1)
                temp1 = "0" & temp1
            Next
        End If
        Return temp1
    End Function

    Function GetNextString(ByVal strBefore As String) As String
        Dim strAwal As String = vbNullString
        Dim temp1 As String = vbNullString
        Dim lnStrBefore As Integer

        lnStrBefore = Len(strBefore)
        Dim a, b As Integer
        Dim cKarakter As String

        b = lnStrBefore + 1
        For a = 1 To lnStrBefore
            b = b - 1
            cKarakter = Mid(strBefore, b, 1)

            If IsNumeric(cKarakter) = False Then
                strAwal = Left(strBefore, b)
                temp1 = Right(strBefore, a - 1)
                temp1 = strAwal & GetNextNumber(temp1)'CALLFUNCTION GetNextNumber
                Exit For
            End If
        Next
        Return temp1
    End Function

    Function FlexibleAutoNumber(ByVal strRecentNumber As String) As String
        If IsNumeric(strRecentNumber) Then
            Return GetNextNumber(strRecentNumber)
        Else
            Return GetNextString(strRecentNumber)
        End If
    End Function
End Class

5.
A brief description of 3 functions above.
a.    Function GetNextNumber used when the layout consists of digits Numeric only
b.    Function GetNextString used when the layout consists of the incorporation of letters and numeric.
c.    FlexibleAutoNumber function, this function will call one of  two functions above according to the type number.


6.
Immediately open the Form1 design, and double click on button1, then fill in the coding below:


Public Class Form1
    Dim c As New clsFunction
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a As String = TextBox1.Text
        TextBox1.Text = c.FlexibleAutoNumber(a)
    End Sub
End Class

7.
Save your project and try to run it, please input sample number, eg INV-0001 after the Save button is clicked then the INV-0001 will turn into INV-0002, and so on.


 After button Save is clicked
8.
Please explore themselves to use this function in accordance with the program you have created. Good luck and hopefully useful.

Just clik Here if want to downlod the Project








Fransiskus Sutris

About Fransiskus Sutris -

Halo, perkenalkan saya Fransiskus Sutris. Saya berbagi tips and trik di blog saya ini dan semoga apa yang saya bagi disini bisa bermanfaat bagi anda yang membacanya.

Subscribe to this Blog via Email :

1 comments:

Write comments
Hermanto
AUTHOR
August 31, 2013 at 7:58 AM delete

Hello,

This is good post from you.. thank you :)

Your function will work well if you only have one PC run this function, and will have problem if runing in some pc and call same function.

for example i have 2 PC Client and access same function, let's say:

- PC1 >> PURCHASE 1 pic is ANI
- PC2 >> PURCHASE 2 pic is BUDI

two pc and have same access function, both division is purchasing.

in 9:53 ANI create po (press button add) and get number PO-0001, and she called by bos to do other job and not close this form(pending this input).

in 10:00 BUDI input PO also, and get number PO-0001, and then save and success, and he's po number is PO-0001.

ANI back and continue her input data, and save and get the problem (your number alrady used by BUDI) :)

your function is not wrong, we only need to recheck again when save, if alrady used need to create new PO.

however this is good post :)

Reply
avatar