+ Reply to Thread
Results 1 to 2 of 2

VB Code to export data from excel to Access

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    01-18-2007
    Location
    Georgia
    MS-Off Ver
    2010
    Posts
    4,434

    VB Code to export data from excel to Access

    Hello:

    Please refer to attached file.
    I need a VB Code to export certain data from excel to MS Access.
    Below is my requirement

    Sheet from where data is to be extracted to be exported : EmpHours
    Data Range A5 to End of month (in this case look for last day of month 11/30/2015)


    Name of Access File Test1.accdb
    Location C:\Temp\ ' This will be the same path as the excel file.
    Table Name : EmpHour


    Let me know if you have any questions.
    Thanks.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    01-18-2007
    Location
    Georgia
    MS-Off Ver
    2010
    Posts
    4,434

    Re: VB Code to export data from excel to Access

    Hello:

    I am trying to use below code which i got from Google but for some reason it is erroring out at 3rd column.
    Please help to find and resolve the problem.

    Note : I am re-attaching the excel file with the code.

    Thanks

    Sub ExportToacess()
    Dim oSelect As Range, i As Long, j As Integer, sPath As String
    
    Sheets("EmpHours").Activate
    Set oSelect = Application.InputBox("Range", , Range("A5").CurrentRegion.Address, , , , , 8)
    
    'MsgBox ("Row: ") & oSelect.Rows.Count
    'MsgBox ("Column: ") & oSelect.Columns.Count
    
    
    
    Dim oDAO As DAO.DBEngine, oDB As DAO.Database, oRS As DAO.Recordset
    ChDir ActiveWorkbook.Path
    'sPath = Application.GetOpenFilename("Access,*.accdb")
    sPath = Application.GetOpenFilename("Access,*.mdb")
    If sPath = "False" Then Exit Sub
    
    Set oDAO = New DAO.DBEngine
    Set oDB = oDAO.OpenDatabase(sPath)
    Set oRS = oDB.OpenRecordset("EmpHour2")
    
    For i = 2 To oSelect.Rows.Count ' Skip lable row
    oRS.AddNew
    
    For j = 1 To oSelect.Columns.Count ' Field(0) is Auto#
      
    '  For j = 1 To 4
      MsgBox ("Column Count: ") & j
      
      oRS.Fields(j) = oSelect.Cells(i, j)
      
      MsgBox ("oRSFields: ") & oRS.Fields(j)
      
      Next j
      oRS.Update
      
      Stop
      Next i
      oDB.Close
      
      If MsgBox("Open the table?", vbYesNo) = vbYes Then
      Dim oAPP As Access.Application
      Set oAPP = New Access.Application
      oAPP.Visible = True
      oAPP.OpenCurrentDatabase sPath
      oAPP.DoCmd.OpenTable "EmpHour2", acViewNormal, acReadOnly
      oAPP.DoCmd.GoToRecord , , acLast
      DoEvents
      End If
      
      
      
      End Sub
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Export Excel Data to Access using VBA
    By RaydenUK in forum Excel General
    Replies: 0
    Last Post: 02-05-2015, 04:52 PM
  2. Export data from excel to Access
    By gunjanagrawal78 in forum Access Tables & Databases
    Replies: 1
    Last Post: 04-25-2014, 09:57 AM
  3. Using Excel VBA to Export data to Ms.Access Table
    By ahmed_one in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-23-2013, 01:54 AM
  4. Opening Excel from Access, running Code in Excel then export back to Access
    By JungleJme in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-28-2012, 08:49 AM
  5. How to export Excel data to an Access table
    By Pixar in forum Excel General
    Replies: 4
    Last Post: 10-28-2008, 01:01 AM
  6. Replies: 0
    Last Post: 07-06-2006, 03:25 PM
  7. export data from Excel to MS Access (ADO) using VBA
    By Peter Brom in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-02-2005, 04:06 PM

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