title: VB to Insert in Access
body: <p>I'm trying to insert data into a MSaccess db using VB but having some trouble finishing the code</p>
  
  <pre><code>Imports System.Data.OleDb
  
  Public Class Form1
  
  Dim provider As String
  Dim dataFile As String
  Dim connString As String
  Public myConnection As OleDbConnection = New OleDbConnection
  Public dr As OleDbDataReader
  
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      'BD Connection
      provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
      dataFile = "C:\Users\Peter\Documents\Database1.accdb"
      connString = provider &amp; dataFile
      myConnection.ConnectionString = connString
  End Sub
  
  Private Sub FindButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindButton.Click
      'Query BD
      myConnection.Open()
      DescriptionText.Clear()
      CostText.Clear()
      PriceText.Clear()
      Dim str As String
      str = "SELECT * FROM Table1 WHERE (Code = '" &amp; CodeText.Text &amp; "')"
      Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
      dr = cmd.ExecuteReader
      While dr.Read()
          DescriptionText.Text = dr("Description").ToString
          CostText.Text = dr("Cost").ToString
          PriceText.Text = dr("Price").ToString
      End While
      myConnection.Close()
  End Sub
  Private Sub AddButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddButton.Click
      'Insert BD
      Dim str As String
  
      myConnection.Open()
      CodeText.Clear()
      DescriptionText.Clear()
      CostText.Clear()
      PriceText.Clear()
  
      str = "INSERT INTO Table1 (Code, Description, Cost, Price) Values (CodeText.Text, DescriptionText.Text, CostText.Text, PriceText.Text) VALUES ('CodeText.Text', 'DescriptionText.Text', 'CostText.Text', 'PriceText.Text')"
      Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
      cmd.ExecuteNonQuery()
  
      MsgBox("saved")
  
      myConnection.Close()
  End Sub
  End Class
  </code></pre>
  
  <p>My query sub work fine but when I try to insert data it gives me an error on the "cmd.ExecuteNonQuery()".</p>
  
  <p>Can you help me find whats wrong?</p>