Output custom query to Excel format in MS Access -


i know there docmd.transferspreadsheet acexport, requires hard query name.

i trying loop on recordset , exporting file per view, example exporting excel file "select * myquery arguments=arg1", file "select * myquery arguments=arg2", , on.

is there way create such excel file based on "custom" on fly sql query this?

use copyfromrecordset dumps vba recordsets excel worksheet range (referencing the upper left corner cell). below subroutine using access vba:

public sub actoxlrecordsets()      dim xlapp object, xlwkb object     dim db database     dim rst recordset     dim args collection, arg variant     dim strpath string, strsql string     dim integer     dim fld field      ' initialize objects      set db = currentdb()     set xlapp = createobject("excel.application")      args.add ("arg1")     args.add ("arg2")     args.add ("arg3")      strpath = "c:\path\to\excel\files"      = 1     each arg in args         ' create new workbook         set xlwkb = xlapp.workbooks.open(strpath & "\excelfile" & i)          ' open new recordset         strsql = "select * myquery arguments = " & arg         set rst = db.openrecordset(strsql, dbopendynaset)         rst.movefirst          ' output recordset         ' first columns         xlwkb.worksheets(1).activate         xlwkb.worksheets(1).range("a1").select          each fld in rst.fields             xlapp.activecell = fld.name             xlapp.activecell.offset(0, 1).select         next          ' next rows         xlwkb.worksheets(1).range("a2").copyfromrecordset rst         xlwkb.worksheets(1).range("a1").select          ' save , close excel workbook         xlwkb.close true          = + 1     next arg      ' uninitialize objects     rst.close     set xlwkb = nothing     set xlapp = nothing     set rst = nothing     set db = nothing  end sub 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -