Autonumber Generator using Visual Basic.Net

In this lesson, I’m going to show you how to generate a customize autonumber using visual basic and Microsoft access as our database. We can use this autonumber to identify a unique record in a table, and it should only one autonumber field is allowed in each table. In our situation, we will generate autonumber by this mechanism start with the start and increment with the increment value. Meaning a Start value plus the Increment value. After completing this course the output of this application it looks like as shown below. To start building this application, add two groupbox, five labels, two buttons and a datagridview.
Object			Property		Settings
Groupbox1		text			Generate Autonumber
Groupbox2		text			list of Autonumbers
Label1			text			Autonumber code:
Label2			text			Autonumber Name:
Label3			text			Append Char:
Label4			text			Autonumber Start:
Label5			text			Increment Value:
Button1		        Name			btngenerate
			Text			Generate
Button2	        	Name			btncancel
			Text 			Cancel

Then, arrange all the object same with the final output shown above. Next, let’s add functionality to our application. Right the form, then click “view code” and add the following code.
  1. 'declare conn as connection and it will now a new connection because
  2.     'it is equal to Getconnection Function
  3.     Dim con As OleDb.OleDbConnection = jokenconn()
  4.     'Represents an SQL statement or stored procedure to execute against a data source.
  5.     Dim cmd As New OleDb.OleDbCommand
  6.     Dim da As New OleDb.OleDbDataAdapter
  7.  
  8.    Public Function jokenconn() As OleDb.OleDbConnection
  9.         Return New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\autonumber.mdb")
  10.     End Function
Then, double click the form and add the following code: This code will simply load all the records from the database table “tblauto” into datagridview.
  1. Try
  2.             con.Open()
  3.             With cmd
  4.                 .Connection = con
  5.                 .CommandText = "Select * from tblauto"
  6.             End With
  7.             Dim publictable As New DataTable
  8.  
  9.             'Gets or sets a Transact-SQL statement or stored procedure used
  10.             'to select records in the data source.
  11.             da.SelectCommand = cmd
  12.             da.Fill(publictable)
  13.             'fill the datagridview by getting fron dt
  14.             DataGridView1.DataSource = publictable
  15.             'hide the first column of datagridview
  16.             DataGridView1.Columns(0).Visible = False
  17.  
  18.          Catch ex As Exception
  19.             MsgBox(ex.Message, MsgBoxStyle.Exclamation)
  20.         End Try
  21.         con.Close()
  22.         da.Dispose()
To insert new autonumber in our database, double click our “btgenerate” button, add the following code:
  1.         Dim result As Integer
  2.         Try
  3.             con.Open()
  4.             With cmd
  5.                 .Connection = con
  6.                 .CommandText = "Insert into tblauto(autocode,autoname,appendchar,autostart,incrementvalue) " & _
  7.                     " Values('" & txtcode.Text & "', '" & txtname.Text & "','" & txtchar.Text & "', " & _
  8.                     " '" & txtstart.Text & "','" & txtinc.Text & "')"
  9.                 result = cmd.ExecuteNonQuery
  10.                 If result = 0 Then
  11.                     MsgBox("No Data has been Inserted!")
  12.                 Else
  13.                     MsgBox("New Autonumber is created succesfully!")
  14.                 End If
  15.             End With
  16.         Catch ex As Exception
  17.             MsgBox(ex.Message, MsgBoxStyle.Information)
  18.  
  19.         End Try
  20.         con.Close()
And you can now test your application by pressing “F5”.

Comments

auto-number? it's just inputting and saving records to database .

Add new comment