Free Source Code and Tutorials

Custom Search

How to Generate Auto Number using Visual Basic.Net and MS Access Database

In this lesson, I’m going to show you how to create a system that will generate a customize auto number using visual basic and Microsoft access as our database. We can use this auto number to identify a unique record in a table, and it should only one auto number field is allowed in each table this will serve as primary key during run time. In our situation, we will generate auto number by this mechanism start with the start and increment with the increment value. It means that from the start value of the auto number, the system will get the last end value then the system will get the increment value and add it to the last ended value to create a new auto number.After completing this course the output of this application it looks like as shown below.


To start building this application, add two Group box, 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 the entire 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
  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
  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
  18. Catch ex As Exception
  19. MsgBox(ex.Message, MsgBoxStyle.Exclamation)
  20. End Try
  21. con.Close()
  22. da.Dispose()

To insert new auto number 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)
  19. End Try
  20. con.Close()

And you can now test your application by pressing “F5”.

You can Download the Source Code here.autonumber

Please this this post. Thank you!

Facebook Comments
(Visited 257 times, 3 visits today)
Share this

Check Your Domain Ranking


Add a Comment
  1. Very shortly this website will be famous among all blogging viewers, due to it’s nice posts

  2. I think this is one of the most vital info for me. And i’m glad reading your article. But want to remark on few general things, The website style is great, the articles is really excellent : D. Good job, cheers

  3. I think this is among the most important information for me. And i’m happy reading your article. But should commentary on some general things, The website taste is ideal, the articles is truly nice : D. Excellent activity, cheers

  4. There’s certainly a lot to find out about this subject. I like all of the points you’ve made.

  5. I’m curious to find out what blog platform you happen to be using? I’m experiencing some minor security problems with my latest website and I’d like to find something more risk-free. Do you have any suggestions?

  6. I got this web site from my pal who informed me on the topic of this site and at the moment this time I am browsing this website and reading very informative articles or reviews at this place.

  7. It’s an awesome piece of writing designed for all the internet viewers; they will take benefit from it I am sure.

    I am genuinely thankful to the owner of this web page who has shared this wonderful piece of writing at here.

  9. Eso prueba que cuando una prueba algo y le va bien, repite de igual modo durante años, por el hecho de que sabe que para ella es eficiente.

  10. Hi sir Joken/Author,

    Do you any contact number or email address?Thanks

Leave a Reply

Your email address will not be published. Required fields are marked * © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!

%d bloggers like this: