仅编辑表中的导入记录

时间:2013-07-24 12:58:45

标签: sql vba ms-access ms-access-2007 access-vba

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

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

1 个答案:

答案 0 :(得分: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