Search code examples
vb.netoledb

how can i insert into table via select data from another table for specific field


I am using OLEDB to connect to a Microsoft Access Database. I want to insert "ItemDesc" field data in "tblCallibrationNewGauge" from "tblItemMst" and another field such as "ItemCode","Customer","Quantity" values taken from users. Below is my code:

 If con.State = ConnectionState.Open Then
  con.Close()
  con.Open()
  Else
 con.Open()
 End If
  Dim cmd As New OleDbCommand("Insert Into tblCallibrationNewGauge(ItemCode,ItemDesc,Customer,Quantity)VALUES('" + partCode.Text.ToString() + "',(Select ItemDesc from tblItemMst where ItemCode='" + partCode.Text.ToString() + "'),'" + cmb_customer.Text.ToString() + "','" + quantity.Text.ToString() + "'",con)
If cmd.ExecuteNonQuery() Then
 MessageBox.Show("Data Inserted")
End If
con.Close()

I'm getting exception error for this. Is it possible to do so? or Is there any way to insert data into table where some fields are taken from other table and some fields taken from winform Control?

Thanks in Advance.


Solution

  • change command text like this:

    Dim cmd As New OleDbCommand("Insert Into tblCallibrationNewGauge( ItemCode,ItemDesc,Customer,Quantity) SELECT '" + partCode.Text.ToString() + "', ItemDesc,'" + cmb_customer.Text.ToString() + "','" + quantity.Text.ToString() + "' from tblItemMst where ItemCode='" + partCode.Text.ToString() + "'",con)