VBA + TransferText + SpecificationName (Access 2007)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5568310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:57:43  来源:igfitidea点击:

VBA + TransferText + SpecificationName (Access 2007)

vbaaccess-vbaspecifications

提问by Vivek

The following is what my DoCmd.TransferText looks like

以下是我的 DoCmd.TransferText 的样子

oCmd.TransferText TransferType:=acExportDelim, _
            SpecificationName:="Schema.ini", _
            TableName:="BASIC_DATA_Query", _
            FileName:="BASIC_DATA_Query_Result.txt", _
            HasFieldNames:=False

When I give the "schema.ini" as my specification name, I get an error

当我将“schema.ini”作为我的规范名称时,出现错误

"Run-time error '3625': The text file specification 'Schema.ini' does not exist. You cannot import, export, or link using the specification."

“运行时错误‘3625’:文本文件规范‘Schema.ini’不存在。您不能使用该规范导入、导出或链接。”

Even after referring to this article: http://support.microsoft.com/kb/241477

即使参考了这篇文章:http: //support.microsoft.com/kb/241477

I have not been able to resolve the problem. My 'Schema.ini' is in the same folder as the DB.

我一直无法解决这个问题。我的“Schema.ini”与数据库位于同一文件夹中。

BASIC_DATA_Query - is a query which has all my results and I need that to be exported to the file BASIC_DATA_Query_Result.txt with header and fields separated by tabs.

BASIC_DATA_Query - 是一个包含我所有结果的查询,我需要将其导出到文件 BASIC_DATA_Query_Result.txt 中,标题和字段由制表符分隔。

What is the possible solution?

可能的解决方案是什么?

回答by Vivek

There seems to be a problem with VBA, a know issue since 2004 :(.

VBA 似乎存在问题,这是自 2004 年以来的一个已知问题:(。

Anyway I wrote code to export and that resolved my problem. Am posting the code here for anyone else

无论如何,我编写了导出代码并解决了我的问题。我在这里为其他人发布代码

Sub ExportTextFileDelimited(FileName As String, _
    DataSet As String, _
    Delimiter As String, _
    TextQualifier As String, _
    WithFieldNames As Boolean)
    On Error GoTo ExportTextFile_Err

    Dim cnn As ADODB.Connection
    Dim rst As New ADODB.Recordset

    Dim Directory As String
    Dim MyString As String, strSQL As String
    Dim strDS As String
    Dim I As Integer

    Open FileName For Output As #1
    Set cnn = CurrentProject.Connection

    rst.Open DataSet, cnn, adOpenForwardOnly, adLockReadOnly
    If WithFieldNames Then
        For I = 0 To rst.Fields.Count - 1
            MyString = MyString & TextQualifier & rst(I).Name & TextQualifier & Delimiter
        Next I
        MyString = Left(MyString, Len(MyString) - 1)
        Print #1, MyString
    End If
    rst.MoveFirst
    Do While Not rst.EOF
        MyString = ""
        For I = 0 To rst.Fields.Count - 1
            'check for text datatype (202)
            If rst(I).Type = 202 Then
                MyString = MyString & TextQualifier & _
                rst(I) & TextQualifier & Delimiter
            Else
                MyString = MyString & rst(I) & Delimiter '<----
            End If
        Next I
            MyString = Left(MyString, Len(MyString) - 2) '<---
        Print #1, MyString & TextQualifier
        rst.MoveNext
    Loop

ExportTextFile_Exit:
    ' Close text file.
    Close #1
    rst.Close
    Set cnn = Nothing
    Exit Sub
ExportTextFile_Err:
    MsgBox Err.Description
    Resume ExportTextFile_Exit
End Sub

Usage:

用法:

Call ExportTextFileDelimited("C:\Query.txt", "Query", vbTab, """", True)

调用 ExportTextFileDelimited("C:\Query.txt", "Query", vbTab, """", True)