Consulting

Results 1 to 5 of 5

Thread: ADODB.Connection Error: Compile Error

  1. #1

    ADODB.Connection Error: Compile Error

    Hello group,
    It has been awhile since I was here, and for the matter done any Excel/VBA work. I am working back in it now that things have settled down.
    I had implemented a little bit of code to grab some data from a closed workbook. It had worked prior, but then somewhere down the line broke.

    The error message is:
    Compile Error: User-defined type not defined


    [VBA]
    Sub GetDataFromClosedWorkbook(SourceFile As String, SourceRange As String, _
    TargetRange As Range, IncludeFieldNames As Boolean)
    ' requires a reference to the Microsoft ActiveX Data Objects library
    ' if SourceRange is a range reference:
    ' this will return data from the first worksheet in SourceFile
    ' if SourceRange is a defined name reference:
    ' this will return data from any worksheet in SourceFile
    ' SourceRange must include the range headers

    <<<<<<<<<<<<<Error is in red>>>>>>>>>>>>>>>>>>>>>>>>
    Dim dbConnection As ADODB.Connection, rs As ADODB.Recordset

    Dim dbConnectionString As String
    Dim TargetCell As Range, i As Integer
    dbConnectionString = "DRIVER={Microsoft Excel Driver (*.xls)};" & _
    "ReadOnly=1;DBQ=" & SourceFile
    Set dbConnection = New ADODB.Connection
    On Error GoTo InvalidInput
    dbConnection.Open dbConnectionString ' open the database connection
    Set rs = dbConnection.Execute("[" & SourceRange & "]")
    Set TargetCell = TargetRange.Cells(1, 1)
    If IncludeFieldNames Then
    For i = 0 To rs.Fields.Count - 1
    TargetCell.Offset(0, i).Formula = rs.Fields(i).Name
    Next i
    Set TargetCell = TargetCell.Offset(1, 0)
    End If
    TargetCell.CopyFromRecordset rs
    rs.Close
    dbConnection.Close ' close the database connection
    Set TargetCell = Nothing
    Set rs = Nothing
    Set dbConnection = Nothing
    On Error GoTo 0
    Exit Sub
    InvalidInput:
    MsgBox "The source file or source range is invalid!", _
    vbExclamation, "Get data from closed workbook"
    End Sub
    [/VBA]


    Thanks,

    Doug
    my site: www.ecboardco.com
    was built w/ a majority of the assistance from the board members here... thanks VBAX.

    Just because I see something, doesn't mean that what's actually happening is what I see.

    You don't get from 0-90 by standing still!

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    That would suggest that you dojn't have a reference to ADO in the VBIDE Doug.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Thanks Bob,
    That pointed me in the right direction. I was able to locate and select teh correct reference and make the connection.
    my site: www.ecboardco.com
    was built w/ a majority of the assistance from the board members here... thanks VBAX.

    Just because I see something, doesn't mean that what's actually happening is what I see.

    You don't get from 0-90 by standing still!

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It's god to see you back Doug, I hope things are good, or as good as they can be.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Thanks Bob....
    my site: www.ecboardco.com
    was built w/ a majority of the assistance from the board members here... thanks VBAX.

    Just because I see something, doesn't mean that what's actually happening is what I see.

    You don't get from 0-90 by standing still!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •