Copy Access Query to Excel via VBA (error 3343) -


i'm attempting copy results of access query , paste excel tab. i've googled around can't seem work, error "error 3343: unrecognized database format" assume has references have checked.

does know correct references need work?

references:

visual basic application

microsoft excel 14.0 object library

ole automation

microsoft office 14.0 object library

microsoft activex data objects 2.8 library

microsft dao 3.6 object library

sub query() dim db dao.database dim rst dao.recordset dim sql string dim icol integer  sheets("datadump1").select selection.clearcontents  end set db = opendatabase("c:\folder\databasename.accdb") set rst = db.openrecordset("query 1")  icol = 1 rst.fields.count  activesheet.cells(1, icol) = rst.fields(icol - 1).name  next icol  activesheet.range("a2").copyfromrecordset rst rst.close db.close set rst = nothing set db = nothing  end sub 

consider calling access object before initializing database , recordset objects. also, use opencurrentdatabase method opendatabase dbengine workspace object.

sub query()     dim accobj object     dim db dao.database     dim rst dao.recordset     dim sql string     dim icol integer      sheets("datadump1").cells.clearcontents      set accobj = createobject("access.application")     accobj.opencurrentdatabase("c:\folder\databasename.accdb")      set db = accobj.currentdb     set rst = db.openrecordset("query 1")      icol = 1 rst.fields.count         sheets("datadump1").cells(1, icol) = rst.fields(icol - 1).name     next icol      sheets("datadump1").range("a2").copyfromrecordset rst     rst.close     db.close      set rst = nothing     set db = nothing     set accobj = nothing  end sub 

alternatively, no need interface access object access database not .exe can connected via odbc/oledb other rdms (oracle, sql server, mysql, etc.)

sub runsql()     dim conn object, rst object     dim strconnection string, strsql string     dim icol integer      set conn = createobject("adodb.connection")     set rst = createobject("adodb.recordset")      sheets("datadump1").cells.clearcontents  '    strconnection = "driver={microsoft access driver (*.mdb, *.accdb)};" _ '                      & "dbq=c:\folder\databasename.accdb;"     strconnection = "provider=microsoft.ace.oledb.12.0;" _                        & "data source='c:\folder\databasename.accdb';"      strsql = " select * [query 1];"      ' open db , recordset     conn.open strconnection     rst.open strsql, conn      ' column headers     icol = 1 rst.fields.count         sheets("datadump1").cells(1, icol) = rst.fields(icol - 1).name     next icol      ' data rows     sheets("datadump1").range("a2").copyfromrecordset rst      rst.close     conn.close     end sub 

Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -