Solved

From Excel to Access VBA

Posted on 2014-10-28
3
305 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 49

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 49

Expert Comment

by:Gustav Brock
ID: 40410568
You are welcome!

/gustav
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now