简体   繁体   English

访问导出查询为带有页眉和页脚的文本

[英]Access export query as text with header and footer

I've been researching this issue high and low for an answer or at least a template to go by.我一直在高低研究这个问题,以获得答案或至少是一个模板。

I am using MS Access 2007. I need to export a query as a text file with fixed width specifications (already done).我正在使用 MS Access 2007。我需要将查询导出为具有固定宽度规格的文本文件(已完成)。 The problem(s) I am running into, is that I must have a specific header and footer appended to the export.我遇到的问题是,我必须将特定的页眉和页脚附加到导出中。 Header must have current date and trailer must have total items being exported.标题必须有当前日期,预告片必须有导出的总项目数。

I am admittedly in over my head, but usually can stumble along with some VBA code that does something similar.不可否认,我很困惑,但通常会遇到一些做类似事情的 VBA 代码。

Can anyone help?任何人都可以帮忙吗?

There isn't any way to define extra lines of text in an export.没有任何方法可以在导出中定义额外的文本行。

I assume you are using the TransferSpreadsheet Method to export your query in fixed-width format.我假设您使用 TransferSpreadsheet 方法以固定宽度格式导出查询。 That's typically the right approach for generating the fixed-width content, with or without field headers.这通常是生成固定宽度内容的正确方法,有或没有字段标题。

But if you want to add lines to the file before and after the data content, then you'll need to open the existing file, create a new file, append the header lines, then append the data from the existing file to the new file, and then append the footer lines, then close both files.但是如果你想在数据内容前后向文件添加行,那么你需要打开现有文件,创建一个新文件,附加标题行,然后将现有文件中的数据附加到新文件中,然后附加页脚行,然后关闭这两个文件。

You could use the built-in VBA functions for working with files, but I find the Scripting.Runtime library offers more intuitive, object-oriented ways of working with files.您可以使用内置的 VBA 函数来处理文件,但我发现 Scripting.Runtime 库提供了更直观、面向对象的文件处理方式。

You'll need to add a reference to the Microsoft Scripting Runtime library in Tools.. References..您需要在 Tools..References.. 中添加对 Microsoft Scripting Runtime 库的引用。

Sub EnhanceExportedFile()

  Const exportedFilePath As String = "C:\Foo.txt"
  Const newFilePath As String = "C:\NewFoo.txt"

  Dim fso As Scripting.FileSystemObject
  Dim exportedFile As TextStream
  Dim newFile As TextStream
  Dim rowCount As Long

  Set fso = New Scripting.FileSystemObject

  Set exportedFile = fso.OpenTextFile(exportedFilePath, ForReading, False)
  Set newFile = fso.CreateTextFile(newFilePath, True)

  'Append the date in ISO format
  newFile.WriteLine Format(Now, "yyyy-mm-dd")

  'Append each line in the exported file
  Do While Not exportedFile.AtEndOfStream
    newFile.WriteLine exportedFile.ReadLine
    rowCount = rowCount + 1
  Loop
  'Append the total exported lines
  newFile.WriteLine rowCount

  'Close both files
  exportedFile.Close
  newFile.Close

End Sub

Use a union query.使用联合查询。 Suppose your query has fields ID (auto number, long), first name, lastname and your tabelname is tablexx.假设您的查询包含字段 ID(自动编号、长)、名字、姓氏,并且您的 tabelname 是 tablexx。 If you have sequential ids it could be something like this:如果您有顺序 ID,它可能是这样的:

Create a query.创建查询。 Select 0 as id, format(date(),"dd/mm/yyyy") as firstname, "" as lastname, "" as nextfield etc etc from tablexx order by id;选择0作为id,格式(日期(),“dd/mm/yyyy”)作为名字,“”作为姓氏,“”作为nextfield等从tablexx order by id;

and a query和一个查询
Select 9999999999 (much bigger than your expected id) as id, (select count(id) from tablexx) as firstname, "" as lastname, "" as nextfield etc etc from tablexx order by id;选择 9999999999(比您预期的 id 大得多)作为 id,(从 tablexx 中选择 count(id))作为名字,"" 作为姓氏,"" 作为 nextfield 等,从 tablexx 按 id 排序;

Now do a union of the three.现在做三者的联合。 Even blank lines can be put in (id = 1 etc).甚至可以输入空行(id = 1 等)。

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

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