简体   繁体   English

如何将多个查询导出到一个Excel工作表中

[英]How to export multiple queries into one Excel worksheet

I have some queries in my Access database. 我的Access数据库中有一些查询。 I know to Export These into one Excel workbook but in different Sheets. 我知道可以将这些导出到一个Excel工作簿中,但可以放在不同的工作表中。 I want to list the results of the queries into one sheet and add one empty row and a caption between the results. 我想将查询结果列出到一张表中,并在结果之间添加一个空行和一个标题。

I don't know how can i handle it, could someone help me? 我不知道该如何处理,有人可以帮我吗?

Set a reference to Microsoft excel in the Access Vb Editor 在Access Vb编辑器中设置对Microsoft Excel的引用

Sub ExportQueries
Dim xl as New Excel.Application  'start up excel
dim wb as workbook
dim ws as worksheet
dim r as range
set wb = xl.workbooks.add      'add a workbook
set ws = wb.worksheets(1)      'point to first sheet
set r = ws.range("a1")         'point to a cell
r = "my first caption"
set r = r.offset(1,0)
'dim rs as new recordset        'ADO
Dim rs as recordset     'DAO
'   rs.open "myquery",currentproject.connection  'ADO
 Set rs = Currentdb.OPenrecordset("myquery")  'DAO


 '*************************Copy field headings into excel
 Dim f as field   
 dim x as integer
 For each f in rs.Fields
    r.offset(0,1)=f.name
    x = x+1
 next f
  set r = r.offset(1,0)
  '****************************End field headings


r.copyfromrecordset rs   'copy results into xl
rs.close
set r = r.end(xldown).offset(2,0) 'point to cell 2 below end of first set of results
r = "my next caption"
set r = r.offset(1,0)
rs.open "myotherquery",currentproject.connection
r.copyfromrecordset rs
rs.close
set r = r.end(xldown).offset(2,0)
'and so on

end sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM