Import Excel data to Access through Excel VBA -


sub accimport()     dim dbconnection adodb.connection     dim dbfilename string     dim dbrecordset adodb.recordset     dim xrow long, xcolumn long     dim lastrow long      'go worksheet containing records want transfer.     worksheets("completed").activate      'determine last row of data based on column a.     lastrow = cells(rows.count, 1).end(xlup).row      'create connection database.     set dbconnection = new adodb.connection      'define database file name     dbfilename = "c:/..."      'define provider , open connection.     dbconnection         .provider = "microsoft.ace.oledb.12.0;data source=" & dbfilename & _                     ";persist security info=false;"         .open dbfilename     end      'create recordset     set dbrecordset = new adodb.recordset      dbrecordset.cursorlocation = aduseserver     dbrecordset.open source:="resolution", _                               activeconnection:=dbconnection, _                               cursortype:=adopendynamic, _                               locktype:=adlockoptimistic, _                               options:=adcmdtable      'loop thru rows & columns load records excel access.     'assume row 1 header row, start @ row 2.     xrow = 2 lastrow         dbrecordset.addnew         'assume 26-column (field) table starting column a.         xcolumn = 1 26             dbrecordset(cells(1, xcolumn).value) = cells(xrow, xcolumn).value         next xcolumn         dbrecordset.update     next xrow      'close connections.     dbrecordset.close     dbconnection.close      'release object variable memory.     set dbrecordset = nothing     set dbconnection = nothing     'optional:     'clear range of data (the records) transferred.     range("a2:z" & lastrow).clearcontents  end sub 

when tried exporting data giving me error in as:

error 3265. item cannot found in collection.

i'm unable export data it's giving me error in line

dbrecordset(cells(1, xcolumn).value) = cells(xrow, xcolumn).value 

... ideas

instead of doing that. use docmd.transferspreadsheet

one line import data access. need add reference microsoft access 14.0 object library able use docmd. or run access

so docmd.transferspreadsheet acexport,acspreadsheettypeexcel12, "accesstablename", "fullfilename",true,"range"

replacing, accesstablename name of table want import to. fullfilename full file name of excel sheet. range range excel sheet. can remove range if want copy excel.

edit. updated open access

dim appaccess access.application     set appaccess = createobject("access.application")      appaccess.visible = true      appaccess.opencurrentdatabase "filename of access db"      appaccess.docmd.transferspreadsheet acimport, acspreadsheettypeexcel12, "accesstablename", "fullfilename", false 

this open access db. run code , import table.


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -