【问题标题】:How to export multiple queries into one Excel worksheet如何将多个查询导出到一个 Excel 工作表中
【发布时间】:2019-04-05 21:18:55
【问题描述】:

我的 Access 数据库中有一些查询。我知道将这些导出到一个 Excel 工作簿中,但在不同的工作表中。我想将查询结果列在一张表中,并在结果之间添加一个空行和一个标题。

我不知道我该如何处理它,有人可以帮助我吗?

【问题讨论】:

  • 我会将每个查询导出到临时表中,将其粘贴到主表中并再次循环。这就是我要做的。
  • 你的临时表是什么意思?
  • 而不是将查询粘贴到您想要的工作表中。将其粘贴到工作表中。例如“TempData”,将 TempData 复制到主工作表中,将所有内容删除到 TempData 中并再次循环。
  • 使用联合查询将多个查询组合成一个结果并将该单个查询导出到 Excel。检查support.office.com/en-us/article/…
  • 从标题中删除标签;商标大写;语法;降噪。

标签: sql vba ms-access


【解决方案1】:

在 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

【讨论】:

  • 嘿,谢谢您的解决方案,我收到“dim rs 作为新记录集”的错误消息,我用“Dim rs As DAO.Recordset Set rs = CurrentDb.OpenRecordset("myquery")”解决了这个问题...
  • 但是我没有用这个代码得到我在 Excel 中的查询结果的列标题,你知道我怎么也可以导出这个吗?
猜你喜欢
  • 1970-01-01
  • 2021-10-21
  • 1970-01-01
  • 2014-02-10
  • 1970-01-01
  • 2011-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多