Solved

From Excel to Access VBA

Posted on 2014-10-28
3
349 Views
Last Modified: 2014-10-29
Hello,

I have a sheet in Excel "RawData" and I have a Access DB with the table "RawData"
I need to write the VBA code (in my Excel macro file) that will move data from my Excel "RawData"
and add it to my Access "RawData". Without opening the Access.

This is what I've wrote so far (getting run time error 3265, Item not found in this collection)

Option Explicit
Sub DB_Export_ToAccess()

  Dim oDAO As DAO.DBEngine
  Dim oDB As DAO.Database
  Dim oRS As DAO.Recordset
 
  Dim lngRow As Long
  Dim intCol As Integer
 
  pubStrDBPath = "\\**************\XcelAccess.mdb"
 
  'Check that DB file exists
  If Dir(pubStrDBPath) = "" Then
    MsgBox "DB Access file " & pubStrDBPath & " Not found."
    Exit Sub
  End If
 
  'Open DB
  Set oDAO = New DAO.DBEngine
  Set oDB = oDAO.OpenDatabase(pubStrDBPath)
  Set oRS = oDB.OpenRecordset("RawData", dbOpenDynaset)
 
  'Populate record set
  For lngRow = 2 To Worksheets("RawData").UsedRange.Rows.Count
    oRS.AddNew
    For intCol = 1 To Worksheets("RawData").UsedRange.Columns.Count
      oRS.Fields(intCol) = Worksheets("RawData").Cells(lngRow, intCol)
    Next intCol
   
    oRS.Update
  Next lngRow
 
  oDB.Close
End Sub
0
Comment
Question by:CABRLU63
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 50

Accepted Solution

by:
Gustav Brock earned 500 total points
ID: 40410089
Field index is zero based, thus:

      oRS.Fields(intCol - 1) = Worksheets("RawData").Cells(lngRow, intCol)

/gustav
0
 

Author Closing Comment

by:CABRLU63
ID: 40410552
Gustav, this works like gold now, thanks!
0
 
LVL 50

Expert Comment

by:Gustav Brock
ID: 40410568
You are welcome!

/gustav
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question