Solved

From Excel to Access VBA

Posted on 2014-10-28
3
335 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
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
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…

730 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