MSAccess:将CSV中的“追加”到访问表中

问题描述 投票:0回答:1

我可以使用以下方法创建新的TABLE,而不会出现问题:

Set db = CurrentDb
strFile = "Item_9766.csv"
strPath = "c:\users\mark\desktop"

strSQL = "SELECT * INTO newTable FROM [Text;HDR=NO;FMT=Delimited;Database=" & strPath & "]." & strFile & ";"
db.Execute (strSQL)

但是,当尝试对现有表使用类似于APPEND的方法时,Access会抱怨“ INSERT INTO语句包含以下未知字段名称:'F1'

strSQL = "Insert INTO myTable Select * FROM [Text;HDR=NO;FMT=Delimited;Database=" & strPath & "]." & strFile & ";"

有任何纠正建议吗? CSV文件以逗号分隔,带有“文本限定符,无标题。对于550K x 50列大型CSV文件,TransferText方法花费的时间太长。

ms-access access-vba
1个回答
0
投票

您可能想在没有'*'的情况下尝试这种方式。

Private Sub InsertRecordsFromCSV()

    Dim cmd As New OleDbCommand
    With cmd
        .CommandText = "INSERT INTO tblTest " & "SELECT ID, ZIP_CODE, LATITUDE, LONGITUDE, CITY, STATE, COUNTY, TYPE " & "FROM [Text;FMT=Delimited;HDR=Yes;DATABASE=" & dataPath & ";].[zips.csv];"
        .CommandType = CommandType.Text
        .Connection = conn
    End With

    Try
        conn.Open()
        cmd.ExecuteNonQuery()
    Catch ex As OleDbException
        MsgBox(ex.ToString)
    Catch ex1 As Exception
        MsgBox(ex1.ToString)
    Finally
        If conn.State = ConnectionState.Open Then conn.Close()
    End Try

End Sub 
© www.soinside.com 2019 - 2024. All rights reserved.