【问题标题】:How to read txt files and import them into access table如何读取txt文件并将其导入访问表
【发布时间】:2019-10-03 06:05:22
【问题描述】:

我是访问 vba 的新手。我正在尝试找到一种将 txt 文件导入我的访问数据库的方法。我阅读了很多文章和论坛,测试了很多代码,但都没有奏效。 我能够缩小到下面列出的代码。我遇到的问题是它运行并运行,然后我关闭了我的数据库并重新开始。没有错误,只是无尽的运行。我的 txt 文件不是那么大,除非我的代码中有错误并且我不知道如何修复它,否则它不应该这样做。 请帮忙。

Dim FileName As String
Dim MostRecentFile As String
Dim MostRecentDate As Date
Dim FileSpec As String
Dim filepath As String
Dim txtStream As Object
Dim strImportRecord As String

filepath = "\\C:\"
 FileSpec = "*.txt*"

FileName = Dir(filepath & FileSpec)

If FileName <> "" Then
    MostRecentFile = FileName
    MostRecentDate = FileDateTime(filepath & FileName)
    Do While FileName <> ""
        If FileDateTime(filepath & FileName) > MostRecentDate Then
             MostRecentFile = FileName
             MostRecentDate = FileDateTime(filepath & FileName)
        End If

       Loop
End If

Set txtStream = CreateObject("Scripting.FileSystemObject").OpenTextFile(MostRecentFile)

Do While Not (txtStream.atendofstream)
        strImportRecord = txtStream.ReadAll
  Loop

DoCmd.TransferText acImportFixed, "myspecification", "mytable", "strImportRecord", False 

【问题讨论】:

  • 帮助文档指出您在哪里拥有“strImportRecord”应该是文本文件的文件名。我不确定它是否可以处理文本字符串。尝试而不是循环遍历文件,只需将“strImportRecord”替换为所需文本文件的路径。另外,请提供文本文件中的数据示例和表格的当前布局。
  • 您是否已单步执行代码以确保其实际定位文件?它是否陷入读取 txtStream 的循环中?你的文件是分隔的吗?您是否已手动导入文件以创建导入规范?
  • Debugging VBA Code 很有帮助。
  • 我不确定我是否理解您关于用文本文件的文件名替换“strImportRecord”的评论,我需要它是一个变量名。请对此进行扩展......我之前手动导入了这个文件,它工作得很好。我也确实通过代码查看代码是否看到文件并且确实如此。我会尝试为你模拟一些文本文件和 tbl 布局。

标签: vba ms-access


【解决方案1】:

试试这个:

Sub ImportMostRecentTextFile()

Dim FileName As String
Dim MostRecentFile As String
Dim MostRecentDate As Date
Dim FileSpec As String
Dim filepath As String
Dim txtStream As Object
Dim strImportRecord As String

filepath = "C:\Users\moone2\Documents\"
 FileSpec = "*.txt*"

FileName = Dir(filepath & FileSpec)

If FileName <> "" Then
    MostRecentFile = FileName
    MostRecentDate = FileDateTime(filepath & FileName)
    Do While FileName <> ""
        If FileDateTime(filepath & FileName) > MostRecentDate Then
             MostRecentFile = FileName
             MostRecentDate = FileDateTime(filepath & FileName)
        End If
        FileName = Dir()
       Loop
End If

'I don't think you need to load the text....
'------------
'Set txtStream = CreateObject("Scripting.FileSystemObject").OpenTextFile(MostRecentFile)'
'
'Do While Not (txtStream.atendofstream)
'    strImportRecord = txtStream.ReadAll
'Loop
'
'Set txtStream = Nothing
'
'Debug.Print strImportRecord

'DoCmd.TransferText acImportFixed, "myspecification", "mytable", strImportRecord,
'---------------

'Just load from the most recent file, like this:
DoCmd.TransferText acImportFixed, "myspecification", "myTable", MostRecentFile, True

结束子

【讨论】:

  • 很高兴听到...很高兴提供帮助。您能否将此标记为解决方案?谢谢!
【解决方案2】:

查找最新文件的循环:

Do While FileName <> ""

永远不会结束,因为您永远不会在循环中为 FileName 分配任何新内容。

您在Loop 之前缺少FileName = Dir()

这不是唯一的问题,迈克在他的 cmets 中写的所有内容都是正确的。

【讨论】:

  • 谢谢。我更新了我的代码,现在 db 给了我一个错误 53 - 找不到文件并突出显示这部分:'''Set txtStream = CreateObject("Scripting.FileSystemObject").OpenTextFile(MostRecentFile)'''
  • 当错误突出显示我的代码时,我将光标移动到 OpenText(MostRecentFile 部分,它显示了我将选择的文件名。这意味着代码可以看到它,但由于某种原因说找不到文件.
  • 谢谢大家。不幸的是,我仍然对这段代码有疑问。我试图更改我的 TransferText 语法,但是当我输入“FileName”而不是 strImportRecord 或 MostRecentFile 时,代码显示该文件不存在,并且当我将鼠标悬停在这部分上时给我空白。任何帮助表示赞赏。
  • @AccessNewbie 那是因为您需要包含路径和文件名。 Dir() 函数只会返回“file.txt”。您需要将其与文件夹路径结合起来。喜欢"C:\Users\user\Documents\" &amp; FileName。还要附加安德烈的答案,循环遍历 Dir() 的结果,只需 FileName = Dir() 没有路径,它将循环遍历从该函数的第一次调用中找到的结果。否则,您并没有真正遍历文件夹中的文件。 docs.microsoft.com/en-us/office/vba/language/reference/…
  • Mike,我对我的代码做了一些更改,FileName = Dir("\\C:\" & MostRecentFile) 和 Set txtStream = CreateObject("Scripting.FileSystemObject").OpenTextFile("\\ C:\ & FileName)。现在这段代码正在不停地运行。我不知道该怎么办。无论如何你可以把代码发给我你会如何纠正它?
猜你喜欢
  • 2018-08-09
  • 1970-01-01
  • 2021-05-25
  • 2018-04-12
  • 2020-02-20
  • 2012-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多