+ Reply to Thread
Results 1 to 5 of 5

VBA code compiling problem...

  1. #1
    Registered User
    Join Date
    06-07-2007
    Posts
    3

    VBA code compiling problem...

    Here is a sample that should do what you want. you will need to adjust the target sheet name, the database and the query SQL.

    Code:
    Sub TransferTableFromAccess()
    Dim cnn As ADODB.Connection
    Dim rst As ADODB.Recordset
    Dim fld As ADODB.Field
    Dim MyConn
    Dim i As Long
    Dim ShDest As Worksheet
    Dim sSQL As String
    Const TARGET_DB = "yourDB.mdb" 'change to suit

    Set ShDest = Sheets("TargetSheet") 'change to suit

    'adjust sSQL to match your table and field names
    sSQL = "SELECT * FROM tblYourTable WHERE YourField = ""Red"""
    Set cnn = New ADODB.Connection
    MyConn = ThisWorkbook.Path & Application.PathSeparator & TARGET_DB

    With cnn
    .Provider = "Microsoft.Jet.OLEDB.4.0"
    .Open MyConn
    End With

    Set rst = New ADODB.Recordset
    rst.CursorLocation = adUseServer
    rst.Open Source:=sSQL, ActiveConnection:=cnn, _
    CursorType:=adOpenForwardOnly, LockType:=adLockOptimistic, _
    Options:=adCmdText

    'clear existing data on the sheet
    ShDest.Activate
    Range("A1").CurrentRegion.Offset(1, 0).Clear

    'create field headers
    i = 0
    With Range("A1")
    For Each fld In rst.Fields
    .Offset(0, i).Value = fld.Name
    i = i + 1
    Next fld
    End With

    'transfer data to Excel
    Range("A2").CopyFromRecordset rst

    ' Close the connection
    rst.Close
    cnn.Close
    Set rst = Nothing
    Set cnn = Nothing

    End Sub


    Denis
    _________________
    An ignoramus is someone who doesn't know something you learned yesterday.

    Back to top

    It is good code, however, I couldn't compile it. I put it to excel book, and it complains that "User defined error not defined"...

  2. #2
    Valued Forum Contributor
    Join Date
    08-26-2006
    Location
    -
    MS-Off Ver
    2010
    Posts
    388
    Setting a reference to the ADO library (Tools>References>Microsoft ActiveX Data Objects 2.x Library) should fix it.

  3. #3
    Registered User
    Join Date
    06-07-2007
    Posts
    3

    Hi

    I setup that, however, that doesn't seem work...

  4. #4
    Registered User
    Join Date
    06-07-2007
    Posts
    3

    There is automation error ...

    The previous problem got fixed, but the new one surface...

  5. #5
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    face_actuary

    Please wrap your VBA code - see red link below for instructions

    Please read all forum rules - see blue link below
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

+ 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