导出查询输出到一个文本文件

编程入门 行业动态 更新时间:2024-10-11 23:15:36
本文介绍了导出查询输出到一个文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有我试图用下面的code导出到文本文件的访问查询:

I have an access query which I am trying to export to a text file using the following code:

DoCmd.TransferText acExportFixed, "Export Specification", _ "Test Query", "C:\Users\Documents\TestOutput.txt", True

我遇到的问题是:输出文件TestOutput.txt与固定宽度显示的数据,但该列标题逗号分隔。我想列标题是固定的宽度了。

The issue I am having is: The output file "TestOutput.txt" has the data displayed with fixed width but the column headers are comma delimited. I want the column headers to be fixed width too.

你会列标题不会显示相同数据的其他人呢?

What would column headers not be displayed same as the rest of the data?

推荐答案

AFAICT,这是TransferText中不可避免的功能。它似乎缺乏任何一种内置智能地说:好吧,我们出口的acExportFixed,所以让我们来看看在导出规范和输出使用这些相同宽度的列标题定义的列宽。相反,它只是给了列名作为一个逗号分隔的列表。

AFAICT, that is an unavoidable "feature" of TransferText. It seems to lack any kind of built-in intelligence to say "OK, we're exporting as acExportFixed, so let's examine the column widths defined in Export Specification and output the column headers using those same widths". Instead it just gives the column names as a comma-separated list.

与其他一切访问,当它的默认行为不满意,您可以编写VBA code做你的方式。

As with everything else in Access, when its default behaviors are unsatisfactory, you can write VBA code to do it your way.

Const VB_FORREADING = 1 Const VB_FORWRITING = 2 Const cstrFile As String = "C:\Users\Documents\TestOutput.txt" Const cstrHeaderRow As String = "col1 col2 etc..." Dim oFSO As Object Dim oFile As Object Dim strContents As String ' do TransferText without the field names ' ' (HasFieldNames default = False) ' DoCmd.TransferText acExportFixed, "Export Specification", _ "Test Query", cstrFile Set oFSO = CreateObject("Scripting.FileSystemObject") ' read file content into strContents string variable ' Set oFile = oFSO.OpenTextFile(cstrFile, VB_FORREADING) strContents = oFile.ReadAll oFile.Close ' re-write file using cstrHeaderRow plus strContents ' Set oFile = oFSO.OpenTextFile(cstrFile, VB_FORWRITING) oFile.write cstrHeaderRow & vbCrLf & strContents oFile.Close Set oFile = Nothing Set oFSO = Nothing

更多推荐

导出查询输出到一个文本文件

本文发布于:2023-11-24 06:00:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624175.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!