Consulting

Results 1 to 9 of 9

Thread: Adding A Reference

  1. #1
    VBAX Regular
    Joined
    Sep 2016
    Posts
    16
    Location

    Adding A Reference

    Hi,

    I am trying to add a reference to Microsoft ActiveX Data Objects 6.1 Library automatically within my code so that it can be distributed to others without them having to add the reference manually. I have heard that late binding may be the solution but I am not sure how to implement it with my current code. Any ideas please?

    Sub SQLQuery()
    
    'Microsoft ActiveX Data Objects 6.1 Library is required
    
    Dim rs As ADODB.Recordset 'holds data
    Dim cnSQL As ADODB.Connection
    Dim sqlString As String
    Dim colOffset As Integer
    Dim Cws As Worksheet
    Dim qf As Object
    colOffset = 0
    
    Set cnSQL = New ADODB.Connection
    cnSQL.Open "Provider=SQLOLEDB.1; Integrated Security = SSPI; Initial Catalog = Database1; Data source = Server1"
    
    sqlString = Range("B1").Value
    
    Set Cws = Worksheets.Add
    
    Set rs = New ADODB.Recordset
    rs.Open sqlString, cnSQL, adOpenStatic, adLockOptimistic
    
    If rs.EOF Then
    MsgBox ("The recordset is empty.")
    End If
    
    For Each qf In rs.Fields 'qf = query field
    Cws.Range("A1").Offset(0, colOffset).Value = qf.Name
    colOffset = colOffset + 1
    Next qf
    
    Cws.Cells(2, 1).CopyFromRecordset rs
    
    rs.Close
    Set rs = Nothing
    
    End Sub
    Last edited by mdmackillop; 04-17-2017 at 11:59 AM. Reason: Code tags added

  2. #2
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    Give this a try:
    Sub SQLQuery()
         
         'Microsoft ActiveX Data Objects 6.1 Library is required
         
        Dim rs As Object
        Dim cnSQL As Object
        Dim sqlString As String
        Dim colOffset As Integer
        Dim Cws As Worksheet
        Dim qf As Object
        colOffset = 0
         
        Set cnSQL = CreateObject("ADODB.Connection") 
        cnSQL.Open "Provider=SQLOLEDB.1; Integrated Security = SSPI; Initial Catalog = Database1; Data source = Server1"
         
        sqlString = Range("B1").Value
         
        Set Cws = Worksheets.Add
         
        Set rs = CreateObject("ADODB.Recordset")
        rs.Open sqlString, cnSQL, adOpenStatic, adLockOptimistic
         
        If rs.EOF Then
            MsgBox ("The recordset is empty.")
        End If
         
        For Each qf In rs.Fields 'qf = query field
            Cws.Range("A1").Offset(0, colOffset).Value = qf.Name
            colOffset = colOffset + 1
        Next qf
         
        Cws.Cells(2, 1).CopyFromRecordset rs
         
        rs.Close
        Set rs = Nothing
         
    End Sub

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    Sub M_snb()
        With CreateObject("ADODB.recordset")
            .Open Range("B1"),"Provider=SQLOLEDB.1; Integrated Security = SSPI; Initial Catalog = Database1; Data source = Server1"
            sheets.add ,sheets(sheets.count)
            Sheets(sheets.count).Cells(1).CopyFromRecordset .DataSource
        End With
    End Sub

  4. #4
    VBAX Regular
    Joined
    Sep 2016
    Posts
    16
    Location
    Quote Originally Posted by JKwan View Post
    Give this a try:
    Sub SQLQuery()
         
         'Microsoft ActiveX Data Objects 6.1 Library is required
         
        Dim rs As Object
        Dim cnSQL As Object
        Dim sqlString As String
        Dim colOffset As Integer
        Dim Cws As Worksheet
        Dim qf As Object
        colOffset = 0
         
        Set cnSQL = CreateObject("ADODB.Connection") 
        cnSQL.Open "Provider=SQLOLEDB.1; Integrated Security = SSPI; Initial Catalog = Database1; Data source = Server1"
         
        sqlString = Range("B1").Value
         
        Set Cws = Worksheets.Add
         
        Set rs = CreateObject("ADODB.Recordset")
        rs.Open sqlString, cnSQL, adOpenStatic, adLockOptimistic
         
        If rs.EOF Then
            MsgBox ("The recordset is empty.")
        End If
         
        For Each qf In rs.Fields 'qf = query field
            Cws.Range("A1").Offset(0, colOffset).Value = qf.Name
            colOffset = colOffset + 1
        Next qf
         
        Cws.Cells(2, 1).CopyFromRecordset rs
         
        rs.Close
        Set rs = Nothing
         
    End Sub
    This says that the variable is not defined when it gets to "adOpenStatic"....

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    Pease do not quote.

  6. #6
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I believe you need to insert the numerical values for such; in this case 3 as googled: adLockOptimistic would also be 3
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    When you use Late Bind, you need to supply your own constants, I do believe.

  8. #8
    VBAX Regular
    Joined
    Sep 2016
    Posts
    16
    Location
    Quote Originally Posted by mdmackillop View Post
    I believe you need to insert the numerical values for such; in this case 3 as googled: adLockOptimistic would also be 3
    Thank you so much, this now works.

  9. #9

Posting Permissions

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