0
Private Sub Command38_Click()
    Dim f As Object
    Dim db As DAO.Database
    Dim qdf As DAO.QueryDef
    Dim strUpdate As String
    Dim strFile As String
    Dim strFolder As String
    Dim varItem As Variant
    Dim P As String
    Dim DeleteEverything As String

        DoCmd.SetWarnings False
        DeleteEverything = "DELETE * FROM [ucppltr]"
        DoCmd.RunSQL DeleteEverything
    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)
    qdf.Parameters("fileName") = strFile
    qdf.Execute dbFailOnError
    Set qdf = Nothing
    Set db = Nothing
    Set f = Nothing

    MsgBox DCount("*", "ucppltr") & " Records were imported"
End Sub

正如您从导入代码中看到的那样,我想存储文件名,虽然它确实有效,但它并不能完全按照我的需要工作。当我们为这个客户工作时,它每周吃一次 5 个文件,所以我希望它保存所有 5 个文件名,但它只保存它导入的最后一个文件名。我的问题,有没有办法将每个文件名保存到每个文件名(我对此表示怀疑)或者我可以将所有 5 个文件名保存到我导入的所有记录中,而不仅仅是最后一个文件名?

我总是可以选择只允许一次导入并使它们导入并附加表 5 次,我只是想在这样做之前检查是否有更有效的方法。

在此先感谢您在此问题上的任何帮助!

4

1 回答 1

1

你的逻辑有问题。在循环内部,strFile 保存当前文件名。因此,在您的循环完成后,只有当前(=last)文件名被传递给查询。

我做了一些更改,因此文件名现在存储在新变量 strFileList 中,由“;”分隔。请检查,如果这是一个可行的解决方案。

Private Sub Command38_Click()
Dim f As Object
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strUpdate As String
Dim strFile As String
Dim strFolder As String
Dim varItem As Variant
Dim P As String
Dim DeleteEverything As String

' Variable to hold file list
Dim strFileList As String

    DoCmd.SetWarnings False
    DeleteEverything = "DELETE * FROM [ucppltr]"
    DoCmd.RunSQL DeleteEverything
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

        'Add file name to file list
        strFileList = strFileList & strFile & ";"
    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") = strFileList

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

MsgBox DCount("*", "ucppltr") & " Records were imported"
End Sub
于 2013-07-19T11:36:19.877 回答