Update Records in Access (2007) Table - Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

In this tutorial, we will update records in Access Database. In this tutorial I create a table with the following fields:

update access table in visual basic

I created the following form:

update access table in visual basic

Add the following code before the (Public Class Form1) line:

Imports System.Data.OleDb

Then add the following declarations below the (Public Class Form1) line

    Dim provider As String
    Dim dataFile As String
    Dim connString As String
    Dim myConnection As OleDbConnection = New OleDbConnection

The following is the Update button click event code:

   Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
        provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
        dataFile = "C:\Users\Jimmy\Documents\Test Database.accdb"
        connString = provider & dataFile
        myConnection.ConnectionString = connString
         myConnection.Open()
        Dim str As String
        str = "update [Items] set [Item] = '" & ItemTxtBox.Text & "' , [Cost] = '" & CostTxtBox.Text & "', [Price] = '" & PriceTxtBox.Text & "', [Quantity] = '" & QtyTxtBox.Text & "' Where [UPC] = '" & UpcTxtBox.Text & "'
                Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
         Try
            cmd.ExecuteNonQuery()
            cmd.dispose()
            myConnection.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
          End Sub



Share This

Home | About | Contact | Privacy Policy

Copyright visual-basic-tutorials.com 2017 - All Rights Reserved.