VBA + TransferText + SpecificationName(Access 2007)

时间:2011-04-06 14:47:06

标签: vba access-vba specifications

以下是我的DoCmd.TransferText看起来像

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

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

  

“运行时错误'3625':文本文件   规范'Schema.ini'没有   存在。您无法导入,导出或   链接使用规范。“

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

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

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

可能的解决方案是什么?

1 个答案:

答案 0 :(得分:1)

自2004年以来,VBA似乎存在一个问题:(。

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

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

用法:

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