Click here to Skip to main content
15,891,650 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to generate auto number like A000012015 and i wnat to increment 000001 to 00999 like this not 00001 to 000099. my code is as under if any other short code for the same please help me
VB
Sub auto()
        Dim A As String = "A0000"
        Dim B As String = Now.Year
        Dim c As String = "1"
        Dim d As String = A + c + B
        Dim f, i, j, k As Integer
        Dim g , h As String
        con.Open()
        com = New OleDbCommand("select autonum from auto", con)
        If com.ExecuteScalar Is DBNull.Value Then
            AutonumTextBox.Text = d
        Else
            'g = com.ExecuteScalar
            dr = com.ExecuteReader
            While dr.Read
                j = Microsoft.VisualBasic.Mid(dr.Item(0), 3, 1)
                f = Microsoft.VisualBasic.Mid(dr.Item(0), 4, 1)
                h = Microsoft.VisualBasic.Mid(dr.Item(0), 5, 1)
                k = Microsoft.VisualBasic.Mid(dr.Item(0), 2, 1)
                i = j & f & h
            End While
            If i >= 999 Then
                g = Microsoft.VisualBasic.Left(A, 1)
                AutonumTextBox.Text = g.ToString + (i + 1).ToString + B
            ElseIf i >= 99 Then
                g = Microsoft.VisualBasic.Left(A, 2)
                AutonumTextBox.Text = g.ToString + (i + 1).ToString + B
            ElseIf i >= 9 Then
                g = Microsoft.VisualBasic.Left(A, 3)
                AutonumTextBox.Text = g.ToString + (i + 1).ToString + B
            Else
                g = Microsoft.VisualBasic.Left(A, 4)
                AutonumTextBox.Text = g.ToString + (i + 1).ToString + B
            End If
        End If
        con.Close()

    End Sub
Posted

1 solution

Try with below code:
VB
Dim totalLength As Integer = 5
Dim g As String
For i As Integer = 1 To 999
	g = "A" + i.ToString().PadLeft(totalLength, "0"C) + DateTime.Now.Year
Next

Output:
A000012015
A000022015
..........
A009992015

Note: As per your question(00001 to 00999), I take total length as 5. So if you need it more then you can change totalLength value.
 
Share this answer
 
v2

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