VB.Net - SPLessons

VB.Net Database Access

Home > Lesson > Chapter 25
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

VB.Net Database Access

VB.Net Database Access

shape Description

Working with the database is always an interesting task in VB.Net why because here first developer has to create a form by dragging requirements based on that code will be generated, but connecting to other databases may give an error, so here first create a table in MS Access then the task will be completed in easy way, but while doing a big project then developer need to connect a database like MySQL, SQL, Oracle... etc...

shape Step - 1

Following is the form needed to be designed in the visual studio.

shape Step - 2

Following is the code to be written to the created form. [vbnet]Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'TODO: This line of code loads data into the 'New_Microsoft_Access_DatabaseDataSet.Table1' table. You can move, or remove it, as needed. Me.Table1TableAdapter.Fill(Me.New_Microsoft_Access_DatabaseDataSet.Table1) End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Table1BindingSource.MovePrevious() End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click Table1BindingSource.AddNew() End Sub Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click Table1BindingSource.MoveNext() End Sub Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click On Error GoTo SaveErr Table1BindingSource.EndEdit() Table1TableAdapter.Update(New_Microsoft_Access_DatabaseDataSet.Table1) MessageBox.Show("Ok BOSS, data has been Saved") SaveErr: Exit Sub End Sub Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click Table1BindingSource.RemoveCurrent() End Sub Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click Me.Close() End Sub End Class [/vbnet]

shape Step-3

Run the above code, then created form will be displayed where user can enter the details as follows, when the user click on the save button data will be saved and saved message will be displayed.

shape Step-4

As like above step user can enter more data to the table as follows, when user click on the save button all the data will be stored in a database. Following is the message will be displayed when user click on save button.

shape Programming Tips

  • When refresh MS-Access database all the related form data will be stored.
  • While using MS-Access don't need to use port numbers and any DB names.
  • AccessDataBase engine should be downloaded to the system before accessing MS-Access database.