I've been trying to store an Excel Worksheet in an Access database and then update the database when changes are made to the worksheet. The main idea that i've had is to use a DataAdapter to fill a DataTable on the SheetChange event. I then use the DataTable.Merge method to update the Access database. The data is uploaded to the database, however when the worksheet changes for the second time, and every time after that, all of the data is uploaded to the database again. The database is not updated, it is just filled with duplicate data.
I tried to fix this by adding an ID column to both the table in the database and the DataTable that is filled by DataAdapter. This uses an AutoNumber and acts as the primary key. I ended up having a lot of problems with not having Update, Delete and Insert Commands on my DataSet in Visual Studio. I managed to solve this problem, but now when I make the second change to the worksheet, I get the following error:
Concurrency violation: the UpdateCommand affected 0 of the expected 1 records.
After hours of googling to try and solve the error, nothing has worked.
Here is my code. All of my SQL commands for Update, Delete and Insert were generated by the Query Builder. I can upload them if you wish. Any ideas or a different approach all together would be much appreciated.
Cheers
I tried to fix this by adding an ID column to both the table in the database and the DataTable that is filled by DataAdapter. This uses an AutoNumber and acts as the primary key. I ended up having a lot of problems with not having Update, Delete and Insert Commands on my DataSet in Visual Studio. I managed to solve this problem, but now when I make the second change to the worksheet, I get the following error:
Concurrency violation: the UpdateCommand affected 0 of the expected 1 records.
After hours of googling to try and solve the error, nothing has worked.
Code:
' update the database each time the worksheet is edited
Private Sub adxExcelEvents_SheetChange(sender As Object, sheet As Object, range As Object) Handles adxExcelEvents.SheetChange
' connect to excel workbook
Dim con As New OleDbConnection
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\Users\Andrew\Desktop\XLBudTemplate.xlsx';Extended Properties=Excel 12.0;"
' tell data adapter to source data from Sheet1 in specified workbook
Dim command As New OleDbCommand()
command.CommandText = "SELECT * FROM[Sheet1$]"
command.Connection = con
' bridge between application and dataset
Dim dataAdapter As New OleDbDataAdapter
' no data is uploaded to the database without the following line
dataAdapter.AcceptChangesDuringFill = False
dataAdapter.SelectCommand = command
dataAdapter.Fill(Table1)
XlBudDBDataSet1.CostCentre1.Merge(Table1)
' update database with new data
CostCentre1TableAdapter1.Update(XlBudDBDataSet1.CostCentre1)
Cheers