473,395 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

beginner update excel ?

I've been looking at working with Excel data.

I understand the process of getting the data into a dataset and modifying
it. It's one of simple beauty that is well documented. Now, I want to send
the updated data set back. I suspect this is also simple but it eludes me.

I have:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim Conn As System.Data.OleDb.OleDbConnection
Conn = New System.Data.OleDb.OleDbConnection( _
"provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source=" & FileName1 & ";" & _
"Extended Properties=Excel 8.0;")
Dim da As New System.Data.OleDb.OleDbDataAdapter("select * update ["
& sheetname1 & "]", Conn)
Try
Dim cmdbldr As New System.Data.OleDb.OleDbCommandBuilder(da)
da.UpdateCommand = cmdbldr.GetUpdateCommand
da.Update(DS)
Conn.Close()
Catch ex As System.Data.OleDb.OleDbException
MsgBox(ex.Message)
End Try
End Sub

Where ds is the dataset and sheetname1="sheet1$".

This code throws the error: missing operator in querry expression
"update[sheet1$]". The code halts on the da.UpdateCommand line when the try
loop is disabled with an unhandled, unspecified
System.Data.OleDb.OleDbException.

I suspect the problem is with the line:

Dim da As New System.Data.OleDb.OleDbDataAdapter("select * update [" &
sheetname1 & "]", Conn)

Perhaps it is incomplete in some way.

I am sure I can loop all of the stuff in the data set back into the excel
sheet with explicit commands, specifying columns and values and all of that.
But how can I simply make the contents of the excel sheet mirror the changed
dataset (in the same simple way I make the dataset mirror the excel sheet to
begin with)??




--
mark b
Feb 14 '06 #1
5 4725
On Tue, 14 Feb 2006 13:24:26 -0800, "mark" <ma**@discussions.microsoft.com> wrote:

¤ I've been looking at working with Excel data.
¤
¤ I understand the process of getting the data into a dataset and modifying
¤ it. It's one of simple beauty that is well documented. Now, I want to send
¤ the updated data set back. I suspect this is also simple but it eludes me.
¤
¤ I have:
¤ Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
¤ System.EventArgs) Handles Button3.Click
¤ Dim Conn As System.Data.OleDb.OleDbConnection
¤ Conn = New System.Data.OleDb.OleDbConnection( _
¤ "provider=Microsoft.Jet.OLEDB.4.0; " & _
¤ "data source=" & FileName1 & ";" & _
¤ "Extended Properties=Excel 8.0;")
¤ Dim da As New System.Data.OleDb.OleDbDataAdapter("select * update ["
¤ & sheetname1 & "]", Conn)
¤ Try
¤ Dim cmdbldr As New System.Data.OleDb.OleDbCommandBuilder(da)
¤ da.UpdateCommand = cmdbldr.GetUpdateCommand
¤ da.Update(DS)
¤ Conn.Close()
¤ Catch ex As System.Data.OleDb.OleDbException
¤ MsgBox(ex.Message)
¤ End Try
¤ End Sub
¤
¤ Where ds is the dataset and sheetname1="sheet1$".
¤
¤ This code throws the error: missing operator in querry expression
¤ "update[sheet1$]". The code halts on the da.UpdateCommand line when the try
¤ loop is disabled with an unhandled, unspecified
¤ System.Data.OleDb.OleDbException.
¤
¤ I suspect the problem is with the line:
¤
¤ Dim da As New System.Data.OleDb.OleDbDataAdapter("select * update [" &
¤ sheetname1 & "]", Conn)
¤
¤ Perhaps it is incomplete in some way.
¤
¤ I am sure I can loop all of the stuff in the data set back into the excel
¤ sheet with explicit commands, specifying columns and values and all of that.
¤ But how can I simply make the contents of the excel sheet mirror the changed
¤ dataset (in the same simple way I make the dataset mirror the excel sheet to
¤ begin with)??

See if the following helps:

How To Use ADO.NET to Retrieve and Modify Records in an Excel Workbook With Visual Basic .NET
http://support.microsoft.com/default...b;EN-US;316934
Paul
~~~~
Microsoft MVP (Visual Basic)
Feb 15 '06 #2
Yes, every thing seems to point to this type of explicit updating where
fields and records must be specified.

However, in Programming VB.NET by Balena (MS Press) in the Chapter 21 "ADO
in Disconnected Mode" pp 1097 et al, an update of the form
da.update(ds,"tableName") appears. Yet, I cannot seem to make that work.

For example, I have code which reads an excel table into a a dataset(ds). A
grid is used to make modifications. I have verified that those modifications
have indeed occured in the dataset. but when I run the following:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim Conn As System.Data.OleDb.OleDbConnection
Conn = New System.Data.OleDb.OleDbConnection( _
"provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source=" & FileName1 & ";" & _
"Extended Properties=Excel 8.0;")
Conn.Open()

da = New System.Data.OleDb.OleDbDataAdapter( _
"select * from [" & sheetname1 & "]",
OleDbConnection1(CurrentIndex))
Dim cmdbldr As New OleDbCommandBuilder(da)
da.InsertCommand = cmdbldr.GetInsertCommand
da.DeleteCommand = cmdbldr.GetDeleteCommand
da.UpdateCommand = cmdbldr.GetUpdateCommand
da.Update(DS, sheetname1)
Conn.Close()
End Sub

I get the error: The DataAdapter.SelectCommand.Connection property needs to
be initialized???

Feb 16 '06 #3
On Thu, 16 Feb 2006 11:07:32 -0800, "mark" <ma**@discussions.microsoft.com> wrote:

¤ Yes, every thing seems to point to this type of explicit updating where
¤ fields and records must be specified.
¤
¤ However, in Programming VB.NET by Balena (MS Press) in the Chapter 21 "ADO
¤ in Disconnected Mode" pp 1097 et al, an update of the form
¤ da.update(ds,"tableName") appears. Yet, I cannot seem to make that work.
¤
¤ For example, I have code which reads an excel table into a a dataset(ds). A
¤ grid is used to make modifications. I have verified that those modifications
¤ have indeed occured in the dataset. but when I run the following:
¤
¤ Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
¤ System.EventArgs) Handles Button3.Click
¤ Dim Conn As System.Data.OleDb.OleDbConnection
¤ Conn = New System.Data.OleDb.OleDbConnection( _
¤ "provider=Microsoft.Jet.OLEDB.4.0; " & _
¤ "data source=" & FileName1 & ";" & _
¤ "Extended Properties=Excel 8.0;")
¤ Conn.Open()
¤
¤ da = New System.Data.OleDb.OleDbDataAdapter( _
¤ "select * from [" & sheetname1 & "]",
¤ OleDbConnection1(CurrentIndex))
¤ Dim cmdbldr As New OleDbCommandBuilder(da)
¤ da.InsertCommand = cmdbldr.GetInsertCommand
¤ da.DeleteCommand = cmdbldr.GetDeleteCommand
¤ da.UpdateCommand = cmdbldr.GetUpdateCommand
¤ da.Update(DS, sheetname1)
¤ Conn.Close()
¤ End Sub
¤
¤ I get the error: The DataAdapter.SelectCommand.Connection property needs to
¤ be initialized???

I don't see code for the DataAdapter's Fill method (on DS). Has it been omitted?
Paul
~~~~
Microsoft MVP (Visual Basic)
Feb 16 '06 #4

I created an original dataadaptor to fill the dataset in a previous sub.

Does my dataadaptor for the update need to be the same dataadaptor I used
for the original fill?
--
mark b
Feb 17 '06 #5
I changed my approach to using a single dataadaptor (da).

My update sub is:
Private Sub Update_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btn_Update.Click
DBCON.Open()
Dim cmdbldr As New OleDbCommandBuilder(da)
da.UpdateCommand = cmdbldr.GetUpdateCommand
da.Update(DS, sheetname1)
DBCON.Close()
End Sub

I now get the error:

Dynamic SQL generation for the DeleteCommand is not supported against a
SelectCommand that does not return any key column information. on the line:

da.UpdateCommand = cmdbldr.GetUpdateCommand

I suspect the problem is that Excel does not provide a primary key.


--
mark b
"Paul Clement" wrote:
On Thu, 16 Feb 2006 11:07:32 -0800, "mark" <ma**@discussions.microsoft.com> wrote:

¤ Yes, every thing seems to point to this type of explicit updating where
¤ fields and records must be specified.
¤
¤ However, in Programming VB.NET by Balena (MS Press) in the Chapter 21 "ADO
¤ in Disconnected Mode" pp 1097 et al, an update of the form
¤ da.update(ds,"tableName") appears. Yet, I cannot seem to make that work.
¤
¤ For example, I have code which reads an excel table into a a dataset(ds). A
¤ grid is used to make modifications. I have verified that those modifications
¤ have indeed occured in the dataset. but when I run the following:
¤
¤ Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
¤ System.EventArgs) Handles Button3.Click
¤ Dim Conn As System.Data.OleDb.OleDbConnection
¤ Conn = New System.Data.OleDb.OleDbConnection( _
¤ "provider=Microsoft.Jet.OLEDB.4.0; " & _
¤ "data source=" & FileName1 & ";" & _
¤ "Extended Properties=Excel 8.0;")
¤ Conn.Open()
¤
¤ da = New System.Data.OleDb.OleDbDataAdapter( _
¤ "select * from [" & sheetname1 & "]",
¤ OleDbConnection1(CurrentIndex))
¤ Dim cmdbldr As New OleDbCommandBuilder(da)
¤ da.InsertCommand = cmdbldr.GetInsertCommand
¤ da.DeleteCommand = cmdbldr.GetDeleteCommand
¤ da.UpdateCommand = cmdbldr.GetUpdateCommand
¤ da.Update(DS, sheetname1)
¤ Conn.Close()
¤ End Sub
¤
¤ I get the error: The DataAdapter.SelectCommand.Connection property needs to
¤ be initialized???

I don't see code for the DataAdapter's Fill method (on DS). Has it been omitted?
Paul
~~~~
Microsoft MVP (Visual Basic)

Feb 17 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: jimserac | last post by:
I had previously posted this in an Access forum with negative results so will try here. Although this question specifies an Access database, I also wish to accomplish this with a large MS SQL...
1
by: jimserac | last post by:
The following SQL statement, used in VBScript, will COPY a table from Excel to an Access mdb. SQL = "SELECT * INTO C1R0" & _ " FROM IN ''" & _ " 'Excel...
1
by: vinay | last post by:
I have read the excel file into the datagrid and modified some data. Now i want to update the excel file with the data. How do I do it. Please help me.
3
by: JacksonYin | last post by:
1. I can fill data from Excel to DataSet like this: OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=../../Book1.xls;Extended Properties=Excel...
3
by: Roy | last post by:
Hi Access gurus, I have a A2K application.The data in the database is updated daily by a excel download.I have a master n related tables keyed in by a OrderID.I have a problem in updating data.If...
1
by: Muskito | last post by:
HELP!!! Hello All, I'm using VB.net 2003 and trying to update data in Excel worksheet. The program selects data from the excel, updates something in the MSSQL DB and then tries to update...
6
by: berndh | last post by:
Hi, I have a need to update all price fields in an SQL database. The new prices are in an excel spreadsheet (c:\db\update.xls). Unfortunately the structure of the Excel file is not the same as...
2
by: ruthboaz | last post by:
Hi, I am exporting a table with data to an excel file.. Some fields are empty in the exported file while the others and the key field, has values. The empty field(s) in excel, will be entered...
22
by: ddg_linux | last post by:
I have been reading about and doing a lot of php code examples from books but now I find myself wanting to do something practical with some of the skills that I have learned. I am a beginner php...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.