-1

In C# I am trying to insert records from one access table to another access table, but I get the above error message. What is causing this error (OleDbException: No value given for one or more required parameters) as it is a straight Select * statement?

OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=P:\\Source.mdb;");
connection.Open();
OleDbCommand command = new OleDbCommand("INSERT INTO [;DATABASE=V:\\Destination.mdb;].[table1] SELECT * FROM table1 WHERE company = 2", connection);
command.ExecuteNonQuery();
connection.Close();

EDIT --- Error in Insert Statement

OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=P:\\Source.mdb;");
connection.Open();
OleDbCommand command = new OleDbCommand("INSERT INTO [;DATABASE=V:\\Destination.mdb;].[table1] (Name, Address, Phone, RepeatCustomer) SELECT Name, Address, Phone, RepeatCustomer FROM table1 WHERE company = 2", connection);
command.ExecuteNonQuery();
connection.Close();
user2676140
  • 1,203
  • 2
  • 14
  • 30

2 Answers2

1

Are the two tables identical? You'd get this error if the column order and/or types don't match. It's much better form to explicitly define the columns you want to modify:

INSERT INTO [;DATABASE=V:\\Destination.mdb;].[table1] (col1,col2) SELECT col1,col2 FROM table1 WHERE company = 2

this way, order doesn't matter, and you aren't trying to add to columns that don't exist.

Matthew
  • 3,560
  • 1
  • 19
  • 44
0

I had the same issue copying to the same table. This sample of just two fields worked for me:

INSERT INTO [Orders] (CUST, PART_NUMBE) SELECT CUST, PART_NUMBE FROM [orders]
WHERE [OrderNumber]= 23979
slfan
  • 8,209
  • 115
  • 61
  • 73