简体   繁体   中英

How do I set up a connection to query data inside an Excel .xls file using Excel 2002?

I am writing a small application in Excel 2002 and I need to store numbers in some format, it can be a string. The tables I have a 1:1 relationship and other table is just a table of one column so using access is not necesary and having to have another file is something I'd like to avoid. So, I want to store it in separate sheets.

However, I like the benefits of SQL for querying and I need it. I tried using some ADODB connection strings to reach this but I cannot achieve it.

I used the following code:

 Dim cn As Object, rs As Object, output As String, sql As String
    
    '---Connecting to the Data Source---
    Set cn = CreateObject("ADODB.Connection")
    With cn
        .Provider = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;"
        .ConnectionString = "Data Source=" & ThisWorkbook.FullName & ";" & "Excel 8.0;HDR=Yes;IMEX=1"
        .Open
    End With

Also, do I have to use ODBC or should I use OLE DB? I don't know if OLE DB could be used to query in excel files.

Also, is it possible to do inserts with SQL using this ODBC or OlE DB? I tried different providers in the connection string, and I checked the ADO references to be available.

Also, I get this error: "Error 3706. The specified provider could not be found. It may not be installed properly."

Connection issue

Since you are on a 64-bit system, you won't be able to use Microsoft.Jet.OLEDB.4.0 for the provider ( more details here ).

This article discussed your alternatives, but it seems like one of them would be Microsoft.ACE.OLEDB.12.0 . I've never used ODBC, but based on this answer , you can't use it to query an Excel file, so OLE DB is the way to go.


Insert Statement

Once you have a working ADODB connection, insert query should work as hoped. I'd suggest to set Mode=ReadWrite in your connection string to avoid potential writting permission issues (but it might work even without it.)

For example, with a workbook named Data.xls with the first sheet named Data and the following data:

在此处输入图像描述

html version for easy copy-paste

I can run the following:

 Dim wb As Workbook Set wb = Workbooks("Data.xls") Dim ws As Worksheet Set ws = wb.Worksheets("Data") 'Create connection Dim conn As New ADODB.Connection conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _ "Data Source=" & wb.FullName & ";" & _ "Extended Properties=""Excel 8.0;HDR=Yes;"";" & _ "Mode=ReadWrite;" 'Compose the INSERT statement. Dim query As String Const sep = ", " query = "INSERT INTO [" & ws.Name & "$] " & _ "(Id, Name, Age) " & _ " VALUES (" & _ 4 & sep & _ "'" & "Joe" & "'" & sep & _ 40 & _ ")" 'Execute the statement. conn.Execute query, adCmdText 'Close the connection conn.Close

And it should insert the data as follow:

在此处输入图像描述

Note that I'm using Excel for Microsoft 365 MSO (Version 2112 Build 16.0.14706.20000) 64-bit on Windows 10.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM