Excel User Group
Microsoft Excel blogs, forums, files. Read, ask questions, provide answers.

Reading excel file with ODBC

Latest post Tue, Jul 8 2008 8:32 AM by Pgmer. 2 replies.
  • Mon, Jul 7 2008 2:43 PM

    • Pgmer
    • Top 50 Contributor
    • Joined on Mon, Mar 31 2008
    • Posts 8
    • Points 136

    Reading excel file with ODBC

    I am trying to use ODBC to read a closed spreadsheet.  The code that I am using (see below) works, but instead of just reading the data, it is actually opening the file and reading the data.  I do not understand why or how.  Any ideas would be greatly appreciated.  Here is the code:

    Public Function read_table(ByVal sSearchVal As String) As String
        Dim sCurrDir As String
        Dim Conn As Connection
        Dim adoConn As ADODB.Connection
        Dim adoRS As ADODB.Recordset
        Dim sTab As String
        Dim sRange As String

        sCurrDir = ThisWorkbook.Path

        If InStr(sCurrDir, scDevmnt) > 0 Then
            sCurrDir = scDevTbl
        Else
            sCurrDir = scProdTbl
        End If

        sTab = "User Area 5"
        sRange = "A1:B6"

        Set adoConn = New ADODB.Connection
        adoConn.Open "DRIVER={Microsoft Excel Driver (*.xls)};ReadOnly=1;DBQ=" & sCurrDir
    '    adoConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sCurrDir & ";Extended Properties=""Excel 8.0;HDR=NO;"""
        Set adoRS = adoConn.Execute("SELECT TRANSLATION FROM [" & sTab & "$" & sRange & "] WHERE NBU='" & sSearchVal & "'")

        If adoRS.EOF Then
            read_table = sSearchVal
        Else
            read_table = adoRS.Fields(0)
        End If

        adoRS.Close
        adoConn.Close
        Set adoRS = Nothing
        Set adoConn = Nothing
    End Function

     I let my mind wander ... it never came back!

    Filed under: ,
    • Post Points: 21
  • Tue, Jul 8 2008 5:41 AM In reply to

    Re: Reading excel file with ODBC

    When you say it is opening it, do you mean that it is opening in Excel, and you can see it? Does it close it as well?

    Regards

    Bob

    • Post Points: 21
  • Tue, Jul 8 2008 8:32 AM In reply to

    • Pgmer
    • Top 50 Contributor
    • Joined on Mon, Mar 31 2008
    • Posts 8
    • Points 136

    Re: Reading excel file with ODBC

     Yes, it is opening it in Excel, just as if you had done a worksheet.open command.  It does NOT close it when the connection closes.

     I let my mind wander ... it never came back!

    • Post Points: 5
Page 1 of 1 (3 items) | RSS
Copyright Excel User Group and the relevant contributors, 2008. All Trade Marks acknowledged. This site is a peer-to-peer site and NOT affiliated in any way to Microsoft Corp. All rights reserved.