how to export excel data to access table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • prasad joshi
    New Member
    • Aug 2012
    • 30

    #1

    how to export excel data to access table

    I am trying to figure out a way to export any new data entered into an Excel 2003 spreadsheet into an Access 2003 database from the Excel application. I've seen lots of code originating in the Access application, but I want to avoid having to go into Access to do it. The users will be entering data into Excel daily, but no one will be in Access daily- and the info needs to be up to date because it is a source for another application(an Infopath 2003 form)which will also be used daily.

    Only the newly entered data would need to be copied, and the primary key would not be in the Excel sheet.

    I'm a newbie to VBA,

    my code which dont work.....

    Code:
    Sub DAOFromExcelToAccess()
        ' exports data from the active worksheet to a table in an Access database
        ' this procedure must be edited before use
        Dim db As DAO.Database, rs As DAO.Recordset, r As Long
        Dim rs2 As DAO.Recordset, lngPK As Long
        ' open the database
        Set db = OpenDatabase("C:\FolderName\DataBaseName.mdb")
        ' open first recordset
        Set rs = db.OpenRecordset("TableName", dbOpenTable)
        ' open second recordset
        Set rs2 = db.OpenRecordset("OtherTableName", dbOpenTable)
        ' get 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...
                ' get the primary key
                lngPK = .Fields("AutoNumField")
                .Update ' stores the new record
            End With
            With rs2
                ' do something with the other recordset here
                ' you can use lngPK
            End With
            r = r + 1 ' next row
        Loop
        rs2.Close
        Set rs2 = Nothing
        rs.Close
        Set rs = Nothing
        db.Close
        Set db = Nothing
    End Sub
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32669

    #2
    Originally posted by Prasad Joshi
    Prasad Joshi:
    my code which dont work.....
    To save my simply deleting the question, which seems a shame as you've not done such a bad job, can you explain what your code is trying to do, what is not working, and in which way it is not working. That means any error messages you see and which line of code it crashes on if it crashes, or what makes you believe it isn't working if it doesn't crash.

    It's obviously difficult for us to answer a question if you don't include any real information in it.

    Comment

    • zmbd
      Recognized Expert Moderator Expert
      • Mar 2012
      • 5501

      #3
      Respectfully, may I inquire as to why the data isn't simply being entered into access directly? A lot of the time, there appears to be no really good reason.

      This seems to be such a common occurance. In fact, in my lab, we started out with Lotus123 then moved to Excel because we designed the benchsheets in the spreadsheet program; thus, it was an easy way to store the data electronically. We had a steep learning curve moving to a database solution because, "We've always done it this way..."
      I've hundreds, maybe thousands of individual workbooks and worksheets - some based on daily (as in new file each day) or monthly (new file each month) data groupings.
      The legacy datamanagement is a total nightmare! I finally did a perminate archive (as in no recovery) of the almost 20 year old lotus files as we nolonger have a way to access that data.

      Comment

      Working...