Insert Into

I'll try to explain this the best I can.  I have a table named "Batches" that I'm trying to insert into one row of data at a time.  In VB.NET I have a dropdown box that displays a number from another column "Code" from another table "Products".  Inside the "Products" table is a column named "ProductID" which corresponds with the Products.Code column.  Here is an example:

Products Table

ProductID     Code
1                 301C
2                 3386
3                 3278
4                 3375
5                 3201

I am trying to insert the ProductID number into "Batches.Product" column.  

In VB.NET my dropdownbox is called "product" and displays the Code from "Products.Code".  My question is, how do I insert into Batches.Product the "ProductID" corresponding with the "Code".

This is what I have so far, but I know it's incomplete:

INSERT INTO Batches (Product)
Values ('" & product.selectedItem.Text & "')"
johnqtrAsked:
Who is Participating?
 
rafranciscoCommented:
Try this:

"INSERT INTO Batches (Product, BatchID)
SELECT ProductID, '25863'
FROM Products
WHERE Code = '" & product.selectedItem.Text & "'"
0
 
rafranciscoCommented:
Try this:

"INSERT INTO Batches (Product)
SELECT ProductID
FROM Products
WHERE Code = '" & product.selectedItem.Text & "'"
0
 
johnqtrAuthor Commented:
Okay, what about if I needed to enter data in more than one column?  Let's say I need to enter 25863 in the "BatchID" column within table "Batches"?  Would I do this?

"INSERT INTO Batches (Product, BatchID)
VALUES ('', '25863')
SELECT ProductID
FROM Products
WHERE Code = '" & product.selectedItem.Text & "'"
0
 
johnqtrAuthor Commented:
Thank You!  That worked great!
0
 
JulianvaCommented:
insert into batches
(productid
,product
,batchid
)

select
productid,
code,
batchid = 25863
from products
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.