Consulting

Results 1 to 6 of 6

Thread: EXCEL to ACCESS

  1. #1
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location

    EXCEL to ACCESS

    The following code is intended to write EXCEL records to ACCESS, however it will not compile. The compile error is on the Set cn =. However cn is defined above. What is up? Thanks in advance for your help!

    Sub ADOFromExcelToAccess()
    ' exports data from the active worksheet to a table in an Access database
    ' this procedure must be edited before use
    Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
        ' connect to the Access database
        Set cn = New ADODB.Connection
        cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
            "Data Source=C:\FolderName\DataBaseName.mdb;"
        ' open a recordset
        Set rs = New ADODB.Recordset
        rs.Open "TableName", cn, adOpenKeyset, adLockOptimistic, adCmdTable
        ' all records in a table
        r = 3 ' the start row in the worksheet
        Do While Len(Range("A" & r).Formula) > 0
        ' repeat until first empty cell in column A
            With rs
                .AddNew ' create a new record
                ' add values to each field in the record
                .Fields("FieldName1") = Range("A" & r).Value
                .Fields("FieldName2") = Range("B" & r).Value
                .Fields("FieldNameN") = Range("C" & r).Value
                ' add more fields if necessary...
                .Update ' stores the new record
            End With
            r = r + 1 ' next row
        Loop
        rs.Close
        Set rs = Nothing
        cn.Close
        Set cn = Nothing
    End Sub
    Peace of mind is found in some of the strangest places.

  2. #2
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    You need to REFERENCE "Microsoft ActiveX Data Objects 2.x Library"

  3. #3
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    example please
    Peace of mind is found in some of the strangest places.

  4. #4
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    In VBE goto Tools - References, scroll down until you see "Microsoft ActiveX Data Objects 2.x Library"

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by austenr
    example please
    Go to the VB IDE - Alt-F11

    Menu Item Tools>References

    In the dialog box that pops up, scroll down to
    "Microsoft ActiveX Data Objects 2.x Library"

    where x is a version number (7 on my 2002 system)

    check that box

    exit, and try again

    (or go late binding)
    ____________________________________________
    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

  6. #6
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Thanks both of you. I found it earlier.
    Peace of mind is found in some of the strangest places.

Posting Permissions

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