Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I get data to Excel?

Talking to Excel

How do I get data to Excel?

by  calculus  Posted    (Edited  )
I've always wondered how to do this, so now that I've figured it out (with direction provided from Camel2021 in a post from 7-22-02) I thought I'd share with everyone else.

You just have to create several objects to use Excel as a database as shown below.

Code:
Dim adoConn as object
Dim RS as object
Dim TableRS as object
Dim strConString as string
Dim FileName as String

Set adoConn = createobject("ADODB.CONNECTION")
Set RS = CreateObject("ADODB.Recordset")
Set TableRS = CreateObject("ADODB.Recordset")
'To be used to enumerate the worksheets in Excel

Path = "C:\MyPath\"
FileName = "MyFile.xls"
DataFile = Path & FileName

    StrConString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
               "Data Source=" & _
               DataFile & ";" & _
               "Extended Properties=""Excel 8.0;HDR=Yes"""
    adoConn.open strConString

    set TableRS = adoConn.OpenSchema(20) 'Get Sheet List

'Enumerate these by looping through the TableRS recordset as normal
    SheetName = TableRS.Fields("TABLE_NAME")

    SQL = "Select * from [" & SheetName & "$] Where [F1] = #" & MyDate & "#"
        RS.open SQL, adoConn, 2,3     'adOpenDynamic, adLockOptimistic
        
        RS.Fields(1).Value = MyValue
        RS.update
        RS.Close

    TableRS.close
    adoConn.close

I hope you find this helpful.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top