Question

Ok so I'm new to .NET and I'm having trouble using the ACE.OLEDB connection to change update values in an Excel worksheet. I keep getting the error: Cannot update '(expression)'; field not updateable. So the structure of my worksheet looks like this:

Symbol   AssetClass  MarketValue  SharePrice
ABC       formula     $1000        $10.50
MSFT      formula     $2000        $12

The formula fields contain a formula that looks up the symbol in another excel workbook and then grabs its asset class. The problem is that it isn't allowing me to update that field. I changed the format of the column to text just to see if that was the problem but no change. I don't know why I can't just add a formula in as if it's text?

Here is my code below:

Private m_sConn1 As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & PATH_Workbook1 & ";Extended Properties=""Excel 12.0 Xml;HDR=YES;MAXSCANROWS=3;READONLY=FALSE\"""
Dim conn As New OleDbConnection(m_sConn1)
conn.Open()
Dim cmd As New OleDbCommand()
cmd.Connection = conn
Dim da As New OleDbDataAdapter("Select * From [HoldingsTest$]", conn)
Dim ds As DataSet = New DataSet()
da.Fill(ds, "HoldingsTable")

' Generate the UpdateCommand and add the parameters for the command.
da.UpdateCommand = New OleDbCommand("UPDATE [HoldingsTest$] SET AssetClass =?, MarketValue =?, SharePrice = ? WHERE Symbol = ?", conn)
da.UpdateCommand.Parameters.Add("@AssetClass", OleDbType.VarChar).SourceColumn = "AssetClass"
da.UpdateCommand.Parameters.Add("@MarketValue", OleDbType.Currency).SourceColumn = "MarketValue"
da.UpdateCommand.Parameters.Add("@SharePrice", OleDbType.Currency).SourceColumn = "SharePrice"
da.UpdateCommand.Parameters.Add("@Symbol", OleDbType.VarChar, 9, "Symbol")

' Update records
ds.Tables(0).Rows(1)("AssetClass") = "formula"
ds.Tables(0).Rows(1)("MarketValue") = 101
ds.Tables(0).Rows(1)("SharePrice") = 91

' Apply the dataset changes to the actual data source (the workbook).
da.Update(ds, "HoldingsTable")

Any help would be greatly appreciated. I'm really stumped here. The code above is a variation on an example from MSDN.

Was it helpful?

Solution

It's mentioned here at How To Use ADO with Excel Data from Visual Basic or VBA ,

"You can edit Excel data with the normal ADO methods. Recordset fields which correspond to cells in the Excel worksheet containing Excel formulas (beginning with "=") are read-only and cannot be edited"

This is also true for OLEDB connection to Excel, unfortunately.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top