我是访问 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