【问题标题】:Importing multiple CSV files into 1 table - first row doesn't match将多个 CSV 文件导入 1 个表 - 第一行不匹配
【发布时间】:2019-10-17 11:52:34
【问题描述】:

我之前使用以下代码将多个 csv 文件从一个文件夹导入到 Access 数据库中的 1 个表中。但是,这次每个文件的第一行包含一个帐号,并且列标题在第 2 行。所以每个文件的第一行不同,并且此代码在“DoCmd.TransferText acImportDelim, , strTable, strPathFile, blnHasFieldNames"

如何排除每个文件的第一行?

我尝试将“blnHasFieldNames”更改为 False,希望代码能够接受第 1 行中的任何差异,但这不起作用。

将 strPathFile 作为字符串、strFile 作为字符串、strPath 作为字符串进行调暗 将 strTable 调暗为字符串,将 strBrowseMsg 调暗为字符串 将 blnHasFieldNames 设为布尔值

    ' Change this next line to True if the first row in EXCEL worksheet
    ' has field names
    blnHasFieldNames = True

    strBrowseMsg = "Table Name"

    strPath = "FilePath"

    If strPath = "" Then
          MsgBox "No folder was selected.", vbOK, "No Selection"
          Exit Sub
    End If

    ' Replace tablename with the real name of the table into which
    ' the data are to be imported
    strTable = "Table Name"

    strFile = Dir(strPath & "\*.csv")
    Do While Len(strFile) > 0
          strPathFile = strPath & "\" & strFile

    DoCmd.TransferText acImportDelim, , strTable, strPathFile, blnHasFieldNames

    ' Uncomment out the next code step if you want to delete the
    ' EXCEL file after it's been imported
    '       Kill strPathFile

          strFile = Dir()
    Loop

我得到的错误是(xxxxxx=我使用了这个而不是帐户名)

运行时错误“2391”: 目标表“表名”中不存在字段“xxxxxxxxx”

【问题讨论】:

    标签: vba ms-access


    【解决方案1】:

    您必须打开文件,删除第一行,保存文件,然后运行导入;或者逐行读取文件,跳过第一行,逐行追加数据。

    在这两种情况下,都可以使用FileSystemObject object

    【讨论】:

      猜你喜欢
      • 2013-09-15
      • 2013-02-05
      • 2012-05-15
      • 1970-01-01
      • 2021-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多