Link to home
Start Free TrialLog in
Avatar of Juan Ocasio
Juan OcasioFlag for United States of America

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("ColumninSpreadsheet") = "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:
    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

Open in new window


Can anyone tell me:
1) is it possible to update the spreadsheet this way and if so,
2) how?

Many thanks!

Juan
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

That option was blocked back in time with Access 2000 due to a patent's right, leaving it read-only.

So you have to use automation to open Excel and the workbook, then write to the worksheet.

/gustav
ASKER CERTIFIED SOLUTION
Avatar of Juan Ocasio
Juan Ocasio
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the feedback.

/gustav
Once you have the recordset, you can just:

Sheets("MySheet").Range("A1").CopyFromRecordset rs

Open in new window

Avatar of Juan Ocasio

ASKER

Figured out the error