My program is throwing the following error
ERROR in UPDATE statement: OleDb (0x80040E14)
and nothing else.
Over the past week I've been having continuous trouble tweaking this code with minor adjustments yet everything always happens to break down.
This is the identical sample that works:
conn.Open();
string sqls = @"INSERT INTO ABCD SELECT * FROM [Excel 12.0;HDR=YES;DATABASE=" + openFileDialog.FileName + "].[" + txtSheetName.Text + "$];";
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandText = sqls;
cmd.ExecuteNonQuery();
string updater =
@"UPDATE ACTB " + @"INNER JOIN ABCD on ACTB.ID = ABCD.ID " +
@"SET ACTB.Started = ABCD.Started, " +
@"ACTB.Ended = ABCD.Ended ";
cmd.CommandText = updater;
cmd.ExecuteNonQuery();
string deleter = @"DELETE from ABCD";
cmd.CommandText = deleter;
cmd.ExecuteNonQuery();
conn.Close();
I just pasted this so that you guys know the logic of the join statement is already sound. I'm adding stuff from an Excel file to an Access database where ID's match. ACTB contains every single field, and has 20 fields of which the rest are irrelevant except for the last four: CTC, POI, DOI, AMT and of course, ID at the start. The excel file is structured the same way: ID, CTC, POI, DOI, AMT with some data and ID data that matches the access database data.
And the code with hiccups:
conn.Open();
string sqls = @"INSERT INTO ACTC SELECT * FROM [Excel 12.0;HDR=YES;DATABASE=" + openFileDialog.FileName + "].[" + txtSheetName.Text + "$];";
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandText = sqls;
cmd.ExecuteNonQuery();
The insert statement alone works, so I know the ACTC table can be read.
string updater =
@"UPDATE ACTB " + @"INNER JOIN ACTC on ACTB.[ID] = ACTC.[ID] " +
@"SET ACTB.[CTC] = ACTC.[CTC], " +
@"SET ACTB.[POI] = ACTC.[POI], " +
@"SET ACTB.[DOI] = ACTC.[DOI], " +
@"ACTB.[AMT] = ACTC.[AMT] ";
cmd.CommandText = updater;
cmd.ExecuteNonQuery();
string deleter = @"DELETE from ACTC";
cmd.CommandText = deleter;
cmd.ExecuteNonQuery();
conn.Close();
Am I looking at another reserved word issue?
What's wrong with my update statement?
And how do I make these error messages make more sense?
The first version of my answer is based on the update syntax from sql server. from your comment, it seems that the update syntax in ms access is a bit different. However, you still have something strange in your update statement - you are using the keyword SET
multiple times. Try this instead:
string updater =
@"UPDATE ACTB " + @"INNER JOIN ACTC on ACTB.[ID] = ACTC.[ID] " +
@"SET ACTB.[CTC] = ACTC.[CTC], " +
@"ACTB.[POI] = ACTC.[POI], " +
@"ACTB.[DOI] = ACTC.[DOI], " +
@"ACTB.[AMT] = ACTC.[AMT] ";
I'm not sure ms-access supports update with inner join, but if it does, then your syntax is a bit off. Try this syntax instead:
UPDATE ACTC
SET ACTB.[CTC] = ACTC.[CTC],
ACTB.[POI] = ACTC.[POI],
ACTB.[DOI] = ACTC.[DOI],
ACTB.[AMT] = ACTC.[AMT]
FROM ACTC
INNER JOIN ACTC on ACTB.[ID] = ACTC.[ID]
User contributions licensed under CC BY-SA 3.0