Results 1 to 3 of 3
  1. #1
    Rameez is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jun 2009
    Location
    Chennai, India
    Posts
    40

    Smile vba to import specific colums on a .xls sheet to a access table

    VBA to import specific tables from a .xls sheet to a access table.

    Currently I have a code that works fine...but that works only if the file and the table are same...I want to import specific colums from a spreadsheet to a access table.



    Any vba code examples are welcome.

    here's the code which i got on googling...but it doesn't run...a error msg on range undefined pops up...Please 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=" & CurrentProject.Path & "\slatool.mdb;"
    ' open a recordset
    Set rs = New ADODB.Recordset
    rs.Open "Temp", 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


    Also in cn.open....i am hardcoding the name of my mbd file...i hope there's a workaround by which...ecen if the user changes the filename....the database will work.

    please help me out.

  2. #2
    jase_kross is offline Novice
    Windows XP Access 2007
    Join Date
    Jul 2009
    Location
    Orange, CA (USA)
    Posts
    7
    Rameez - I hope that since your post you have been able to get your code running.

    Anyhow, I wanted to make some comments about the code in your post.

    An attempt to execute the block

    Code:
    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=" & CurrentProject.Path & "\slatool.mdb;"
        ' open a recordset
        Set rs = New ADODB.Recordset
        rs.Open "Temp", 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
    from within Access could result in a sub or function not defined error as 'Range' is not part of the Access Object model but rather it is part of the Excel Object model. Also, as the comments from the block seem to imply, the intention of the procedure is to export data from the active worksheet to a table in an Access database. This implies that the above procedure is/was meant to be used from within Excel.

    I tested the above block in an Excel workbook (set reference to ADO) with the code placed in the WorkBook module of the workbook. I also set up a "Temp" table in a slatool.mdb with the fields: FieldName1, FieldName2, and FieldName3 (all of type 'text') and placed the slatool.mdb file within the same directory as the Excel workbook. I only changed the code 'CurrentProject.Path' (highlighted in red) above to 'ThisWorkbook.Path' as CurrentProject.Path is not defined within the Excel Object model. Once I made this adjustment I was able to execute the code without any problems (the database should be closed) from within Excel.

  3. #3
    Rameez is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jun 2009
    Location
    Chennai, India
    Posts
    40

    Smile

    yeah i did it...i first put all the data in an excel into a access table and then ran query over that table...

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 0
    Last Post: 04-29-2009, 04:27 PM
  2. Import XML String into Access table
    By chrisjohns in forum Programming
    Replies: 0
    Last Post: 04-16-2009, 01:47 AM
  3. Automate Excel Import to Access Related Tables
    By KramerJ in forum Programming
    Replies: 6
    Last Post: 04-04-2009, 04:24 PM
  4. Import Lotus Notes address book into a MS Access table.
    By acheo in forum Import/Export Data
    Replies: 0
    Last Post: 02-19-2007, 02:43 PM
  5. Unable to import or link tables through odbc in Access SP2
    By Dave Jenkins in forum Import/Export Data
    Replies: 3
    Last Post: 11-09-2005, 11:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums