Solved

From Excel to Access VBA

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
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…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

856 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