Updating Records in MS Access Using VB.Net
This tutorial is all about Updating Records in MS Access Using VB.Net. In this post i will teach you on Updating Records in MS Access Using VB.Net. So let’s get started:
As a continuation of this tutorial. We will use the Displaying Data Table Records in Datagridview using VB.Net
- First, Design your Form like this one below.
Add another Button. Name it “Update“
- After designing the form. Click the Update Button and add this following codes.
[vbnet]
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Try
Sql = “UPDATE tblitems SET ITEMNAME='” & TextBox1.Text & “‘,BRAND= ‘” & TextBox2.Text & “‘, ” & _
” PRICE='” & TextBox3.Text & “‘ WHERE ID = ” & Me.Text
Myconnection.Open()
With dbcmd
.CommandText = Sql
.Connection = Myconnection
End With
result = dbcmd.ExecuteNonQuery
If result > 0 Then
MsgBox(“New Record has been Updated!”)
Myconnection.Close()
Call Button1_Click(sender, e)
cleartextfields()
Else
MsgBox(“No Record has been Updated!”)
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
Myconnection.Close()
End Try
End Sub
[/vbnet]
- After that, Add this following Sub in the Public Class Form1.[vbnet]
Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Me.Text = DataGridView1.CurrentRow.Cells(0).Value.ToString
TextBox1.Text = DataGridView1.CurrentRow.Cells(1).Value.ToString
TextBox2.Text = DataGridView1.CurrentRow.Cells(2).Value.ToString
TextBox3.Text = DataGridView1.CurrentRow.Cells(3).Value.ToString
End Sub
[/vbnet] - Then Click F5 to Run the Program.
Output.