【问题标题】:Set Field Value when importing CSV in Access在 Access 中导入 CSV 时设置字段值
【发布时间】:2020-07-05 22:14:58
【问题描述】:

我正在使用 Access VBA 和传输文本方法导入数千个 CSV 文件。我需要将文件名或它的某种格式添加到表中以反映它来自的文件。我已经存储了变量文件名以便导入它。如何附加代码以引用“fil”变量的名称?

With DoCmd
    .SetWarnings False
    For Each fil In fld.Files
        If UCase(Right(fil.Name, 3)) = "CSV" Then
            .TransferText acImportDelim, , DestTable, fil.Path, False



        End If

    Next
    .SetWarnings True
End With

【问题讨论】:

    标签: vba csv ms-access


    【解决方案1】:

    试试这个:

    loopCounter = 1
    With DoCmd
        .SetWarnings False
        For Each Fil In fld.Files
                'I'm assuming the new field that will hold the FileName
                'doesn't exist as part of the original table so we
                'are going to create it for the first file read in
            If loopCounter = 1 Then CurrentDb.Execute ("Alter Table MyTable Add Column MyNewField Text")
    
            If UCase(Right(Fil.Name, 3)) = "CSV" Then
                .TransferText acImportDelim, , DestTable, Fil.Path, False
                CurrentDb.Execute ("Update MyTable Set MyNewField = '" & Fil.Name & "' where MyNewField is null")
            End If
    
            loopCounter = loopCounter + 1
        Next
        .SetWarnings True
    End With
    

    【讨论】:

      猜你喜欢
      • 2013-12-18
      • 1970-01-01
      • 1970-01-01
      • 2021-04-16
      • 2013-12-25
      • 2013-04-04
      • 1970-01-01
      • 1970-01-01
      • 2021-04-11
      相关资源
      最近更新 更多