【发布时间】:2020-07-13 13:47:38
【问题描述】:
我目前有一个使用访问规范导入文件夹中所有文件的宏。我想在 Access 表的末尾添加一列,其中包含每个导入的 .txt 文件的文件名。到目前为止,这是我的代码。如何完成?
Sub cmdImport_Click()
Dim strPath As String
Dim strFile As String
Dim strTable As String
Dim strSpecification As String
Dim intImportType As AcTextTransferType
Dim blnHasFieldNames As Boolean
strTable = "Reconstrucción"
strSpecification = "Reconstruir"
blnHasFieldNames = False
intImportType = acImportDelim
' Permite al usuario elegir una carpeta
With Application.FileDialog(4)
If .Show Then
strPath = .SelectedItems(1)
Else
MsgBox "No seleccionó una carpeta", vbExclamation
Exit Sub
End If
End With
If Right(strPath, 1) <> "\" Then
strPath = strPath & "\"
End If
DoCmd.OpenForm "frmMessage"
Forms!frmMessage.Repaint
' Loop a través de los archivos de texto
strFile = Dir(strPath & "*.txt")
Do While strFile <> ""
' Importa los archivos de texto de la carpeta
DoCmd.TransferText _
TransferType:=acImportFixed, _
SpecificationName:=strSpecification, _
TableName:=strTable, _
FileName:=strPath & strFile, _
HasFieldNames:=blnHasFieldNames
strFile = Dir
Loop
DoCmd.Close acForm, "frmMessage"
End Sub
【问题讨论】: