首页 > 解决方案 > 如何将 ACCESS DB 中的 sql 查询结果写入 txt 文件?

问题描述

MS Office 365 专业增强版,访问 2007 - 2016

我需要能够捕获 Access DB 的查询结果并将其发送到 C 驱动器上的 txt 文件。这将使用任务调度程序(我在想)将运行某种脚本以 1x/wk 完成。我发现了不同的基于 SQL Server 的示例,但对于 Access 却没有。

谁能提供一个如何做到这一点的例子?

标签: ms-access

解决方案


1.从任务调度程序中,您实际上可以调用可以触发模块的宏。研究这个,因为我不知道我头顶的语法,但它真的很容易(只需在它之后使用 ax/callmymacro 命令执行应用程序)

创建文本文件非常简单。您可以像上面提到的那样使用 DoCmd.TransferText 导出,您可以在代码中构建它,特别是如果查询结果没有所有内容或者您只需要某些部分。这是一个快速而肮脏的模板。

'this is only a template for proof of concept for you - google syntax of how to appropriately
'declare and use recordsets. Same with declaring other variables
'if your text file requieres headers, google how to loop through recordset headers.
'Pretty easy stuff

sep = "|"  'your choice of delimiter
NewTextFile = "pathwhereyouwanttodropyourfile.FileNametxt"
Open NewTextFile For Output As #2

do until rs.eof
    'wholeLine is a string variable used to store the currentline of the text file. 
    WholeLine  = WholeLine & rs("filed1") & sep                    
    Print #2, WholeLine
    WholeLine = "" 'important to reset variable
    rs.movenext
Loop

Close #2

*如果你弄清楚如何使用数组而不是循环遍历记录集,你会得到很酷的分数。*


推荐阅读