【问题标题】:How can I write sql query results from an ACCESS DB to a txt file?如何将 ACCESS DB 中的 sql 查询结果写入 txt 文件?
【发布时间】:2019-05-07 19:23:37
【问题描述】:

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

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

谁能提供一个例子来说明如何做到这一点?

【问题讨论】:

  • 问题有两部分:1)如何运行一个脚本,该脚本将调用Access stackoverflow.com/questions/40999718/…中的某些程序; 2)VBA代码导出文件,探索DoCmd.TransferText或文本文件对象读/写。

标签: ms-access


【解决方案1】:

1.从任务调度程序中,您实际上可以调用可以触发模块的宏。研究这个,因为我不知道我头顶的语法,但它真的很容易(只需在它之后使用 x/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

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

【讨论】:

    猜你喜欢
    • 2016-09-07
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-08
    • 1970-01-01
    • 2014-04-27
    相关资源
    最近更新 更多