目标表中不存在该字段

问题描述

我有一个备份系统,出于各种原因,该系统会将表中的数据导出到.txt文件。我使用文本流对象,因为我在使用通常的导出方法截断小数位数时遇到了问题(这是一个众所周知的问题):

Sub ExportTables(fso As Object,exportPath As String,arr() As String)

Dim x As Integer
Dim recSet As Recordset
Dim fieldString As String
Dim fld As Variant
Dim txtStream As Object

For x = 0 To UBound(arr)
    
    fso.CreateTextFile exportPath & "\" & arr(x) & ".txt"
    Set txtStream = fso.OpenTextFile(exportPath & "\" & arr(x) & ".txt",8,Format:=TristateTrue)
    
    Set recSet = CurrentDb.OpenRecordset(arr(x))
    With recSet
    
        fieldString = ""
        For Each fld In .fields
            fieldString = fieldString & "," & Chr(34) & fld.Name & Chr(34)
        Next fld
        fieldString = Right(fieldString,Len(fieldString) - 1)
        txtStream.WriteLine fieldString
    
        While Not .EOF
        
            fieldString = ""
            For Each fld In .fields
                If fld.Type = dbText Or fld.Type = dbMemo Then
                    fieldString = fieldString & "," & Chr(34) & fld.value & Chr(34)
                Else
                    fieldString = fieldString & "," & fld.value
                End If
            Next fld
            
            fieldString = Right(fieldString,Len(fieldString) - 1)
            
            txtStream.WriteLine fieldString
        
            .movenext
        Wend
        
    End With
    
    txtStream.Close

Next x

End Sub

这可以很好地创建文件。但是,当我尝试导入这些文件时,出现错误

目标表'The_Table_In_Question'中不存在字段'ÿþ'。

使用代码

DoCmd.TransferText acImportDelim,TableName:=arr(x),Filename:=fp & arr(x) & ".txt",HasFieldNames:=True

无论使用哪个表,都会发生相同的错误包括相同的奇怪字段名称“ name”。

我已经通过外部数据对话框将一些备份文件上传到新表中,每次导入都没有问题,并且没有意外字段。

我将这些备份文件中的粘贴数据复制到新的文本文件中(从字面上全选,复制,粘贴),并使用上面的代码很好地上传了这些文件,因此我认为这会导致某种不可打印的字符异常文字流。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)