1
Set f = Application.FileDialog(3)
f.AllowMultiSelect = True
f.InitialFileName = "S:\Formware\outfile\ucppt12\Storage"
f.Filters.Clear
f.Filters.Add " Armored TXT Files", "*.asc"
    If f.Show Then
    For Each varItem In f.SelectedItems
        strFile = Dir(varItem)
        strFolder = Left(varItem, Len(varItem) - Len(strFile))
        P = strFolder & strFile
        DoCmd.TransferText acImportDelim, "UCPP Import Specification", "ucppltr", P, False

    Next
    End If
strUpdate = "PARAMETERS fileName Text;" & vbCrLf & _
"UPDATE ucppltr" & vbCrLf & _
"Set [File Name] = fileName"
Debug.Print strUpdate
Set db = CurrentDb
Set qdf = db.CreateQueryDef("", strUpdate)

'Pass file list to query
qdf.Parameters("fileName") = strFile

qdf.Execute dbFailOnError
Set qdf = Nothing
Set db = Nothing
Set f = Nothing

如您所见,我通常一次使用 5-6 个左右导入文本文件MultiSelect。我有一个文件名字段,并希望根据从中导入记录的文件来填充它。到目前为止,我只用最后一个文件名覆盖前 4 或 5 个文件名,为所有记录提供相同的文件名。我不确定是否有办法在导入时通过TransferText或任何其他方式填充该字段。

4

1 回答 1

1

修改您的UPDATE语句以包含WHERE [File Name] Is Null.

然后,在每个 之后,立即TransferText将当前文件名提供给UPDATE查询并执行它。

strUpdate = "PARAMETERS fileName Text;" & vbCrLf & _
    "UPDATE ucppltr" & vbCrLf & _
    "Set [File Name] = fileName" & vbCrLf & _
    "WHERE [File Name] Is Null;"
Debug.Print strUpdate
Set db = CurrentDb
Set qdf = db.CreateQueryDef("", strUpdate)

Set f = Application.FileDialog(3)
f.AllowMultiSelect = True
f.InitialFileName = "S:\Formware\outfile\ucppt12\Storage"
f.Filters.Clear
f.Filters.Add " Armored TXT Files", "*.asc"
If f.Show Then
    For Each VarItem In f.SelectedItems
        strFile = Dir(VarItem)
        strFolder = Left(VarItem, Len(VarItem) - Len(strFile))
        p = strFolder & strFile
        DoCmd.TransferText acImportDelim, _
            "UCPP Import Specification", "ucppltr", p, False
        'Pass file name to the query
        qdf.Parameters("fileName") = strFile
        qdf.Execute dbFailOnError
    Next
End If
于 2013-07-24T16:11:28.490 回答