Juan Ocasio
asked on
Using ADO to write to to an excel spreadsheet via Access 2010
Hello all:
I am opening a recordset and connecting to an excel spreadsheet using Access 2010. What I am trying to do is read from it as well as write to it. I can query the recordsource (a sheet in the workbook via SELECT * FROM [Sheets1$] ), but can't write to it. I was trying to write to it by using the following method:
rs.Fields("ColumninSpreads heet") = "someValue"
and then performing a rs.Update. This does not error out, however it does not update the column in the excel file. Here is some of the code I currently have:
Can anyone tell me:
1) is it possible to update the spreadsheet this way and if so,
2) how?
Many thanks!
Juan
I am opening a recordset and connecting to an excel spreadsheet using Access 2010. What I am trying to do is read from it as well as write to it. I can query the recordsource (a sheet in the workbook via SELECT * FROM [Sheets1$] ), but can't write to it. I was trying to write to it by using the following method:
rs.Fields("ColumninSpreads
and then performing a rs.Update. This does not error out, however it does not update the column in the excel file. Here is some of the code I currently have:
con.Provider = "Microsoft.ACE.OLEDB.12.0" ' "Microsoft.Jet.OLEDB.4.0"
con.ConnectionString = "Data Source = " & strFileName & ";" & _
"Extended Properties=Excel 12.0;" ' "Extended Properties = Excel 8.0;"
con.Open
strSQL = "SELECT * FROM [" & lstSheetNames.value & "$]"
rs.Open strSQL, con, adOpenStatic, adLockBatchOptimistic
If Not (rs.EOF And rs.BOF) Then
Do Until rs.EOF
rs.Fields("UserName") = "userName"
rs.Update
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
con.Close
Set con = Nothing
Can anyone tell me:
1) is it possible to update the spreadsheet this way and if so,
2) how?
Many thanks!
Juan
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thanks for the feedback.
/gustav
/gustav
Once you have the recordset, you can just:
Sheets("MySheet").Range("A1").CopyFromRecordset rs
ASKER
Figured out the error
So you have to use automation to open Excel and the workbook, then write to the worksheet.
/gustav