+ Reply to Thread
Results 1 to 3 of 3

Referencing Libraries through code.

  1. #1
    Purnima
    Guest

    Referencing Libraries through code.

    Hi,

    I have a tool developed in VBA which references the ActiveX Data Obj Library
    2.8. Now this code runs fine on some machines but on others it gives a
    reference error. So I have to then add the reference to the ADO library on
    that machine and run the code. Is it possible to overcome this problem in
    some way and run the code on all machines without any error? If I somehow add
    the libraries through coding, will it help? And if yes, how do I add the
    libraries through code itself rather than going to the Tools menu and
    selecting the reference?

    Any help is appreciated.

    Thanx.

  2. #2
    Bob Phillips
    Guest

    Re: Referencing Libraries through code.

    You could do one of two things:

    Install the code on the lowest version of Excel that you will deploy it to,
    and recompile until you have no errors, and then deploy to the later
    versions.

    Use lat binding. With this you don't set a reference in the project, VBA
    will get the library at run-time. For late binding, you need to declare all
    of the specific objects types as Object, and change any of those library
    constants to their numeric equivalent. So for instance, instead of

    Dim oConn As ADODB.Connection
    Dim oRS As ADODB.Recordset
    Dim sFilename As String
    Dim sConnect As String
    Dim sSQL As String

    sFilename = "c:\Mytest\Volker1.xls"
    sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & sFilename & ";" & _
    "Extended Properties=Excel 8.0;"

    sSQL = "SELECT * FROM [Sheet1$]"

    sSQL = "SELECT * FROM [Sales$A1:E89]"
    Set oRS = New ADODB.Recordset
    oRS.Open sSQL, sConnect, adOpenForwardOnly, _
    adLockReadOnly, adCmdText

    ' Check to make sure we received data.
    If Not oRS.EOF Then
    Sheet1.Range("A1").CopyFromRecordset oRS
    Else
    MsgBox "No rec"
    End If


    You might use

    Dim oConn As Object
    Dim oRS As Object
    Dim sFilename As String
    Dim sConnect As String
    Dim sSQL As String

    Const adOpenForwardOnly As Long = 0
    Const adLockReadOnly As Long = 1
    Const adCmdText As Long = 1

    sFilename = "c:\Mytest\Volker1.xls"
    sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & sFilename & ";" & _
    "Extended Properties=Excel 8.0;"

    sSQL = "SELECT * FROM [Sheet1$]"

    Set oRS = CreateObject("ADODB.Recordset")

    sSQL = "SELECT * FROM [Sales$A1:E89]"
    oRS.Open sSQL, sConnect, adOpenForwardOnly, _
    adLockReadOnly, adCmdText

    ' Check to make sure we received data.
    If Not oRS.EOF Then
    Sheet1.Range("A1").CopyFromRecordset oRS
    Else
    MsgBox "No rec"
    End If


    This is covered, albeit for an Outlook connection, at
    http://www.xldynamic.com/source/xld.EarlyLate.html

    --

    HTH

    RP
    (remove nothere from the email address if mailing direct)


    "Purnima" <[email protected]> wrote in message
    news:[email protected]...
    > Hi,
    >
    > I have a tool developed in VBA which references the ActiveX Data Obj

    Library
    > 2.8. Now this code runs fine on some machines but on others it gives a
    > reference error. So I have to then add the reference to the ADO library on
    > that machine and run the code. Is it possible to overcome this problem in
    > some way and run the code on all machines without any error? If I somehow

    add
    > the libraries through coding, will it help? And if yes, how do I add the
    > libraries through code itself rather than going to the Tools menu and
    > selecting the reference?
    >
    > Any help is appreciated.
    >
    > Thanx.




  3. #3
    RB Smissaert
    Guest

    Re: Referencing Libraries through code.

    There is a third option that works fine for me, but I am not sure it always
    works.
    Save the workbook without the ADO reference.
    Via the workbook Open event run this Sub:

    Sub AddADO()

    Dim R

    For Each R In ThisWorkbook.VBProject.References
    If R.GUID = "{00000205-0000-0010-8000-00AA006D2EA4}" And R.Major = 2
    Then
    Exit Sub
    End If
    Next

    On Error GoTo NOTFOUND

    'although usually the ADO version will be higher, doing Minor:=0 will
    install
    'the higher version if available. On the other hand when you specify
    Minor:=5
    'and only a lower version is available, this can't be installed
    '----------------------------------------------------------------------------
    ThisWorkbook.VBProject.References.AddFromGuid _
    GUID:="{00000205-0000-0010-8000-00AA006D2EA4}", _
    Major:=2, Minor:=0
    Exit Sub

    NOTFOUND:
    On Error GoTo 0

    End Sub

    This will add the ADO reference that is available on the user's machine.


    So, you will get:

    Private Sub Workbook_Open()
    AddADO
    End Sub


    RBS


    "Purnima" <[email protected]> wrote in message
    news:[email protected]...
    > Hi,
    >
    > I have a tool developed in VBA which references the ActiveX Data Obj
    > Library
    > 2.8. Now this code runs fine on some machines but on others it gives a
    > reference error. So I have to then add the reference to the ADO library on
    > that machine and run the code. Is it possible to overcome this problem in
    > some way and run the code on all machines without any error? If I somehow
    > add
    > the libraries through coding, will it help? And if yes, how do I add the
    > libraries through code itself rather than going to the Tools menu and
    > selecting the reference?
    >
    > Any help is appreciated.
    >
    > Thanx.



+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1