在Access VBA中转义双引号 - INSERT INTO ... SELECT

Ral*_*and 5 csv ms-access vba excel-vba access-vba

我拥有以下VBA代码,将许多文本文件放到Access表中.但是对于包含带双引号的文本的.TXT文件的情况存在问题,因此使用空值中断该记录的所有其他字段.

我试图在选择"产品"字段时添加"替换"功能,但不适用于"双引号".使用其他字符,但双引号(否)...

你推荐哪些调整?任何建议,将不胜感激.

*注意:实际数据超过100万条记录......

SCHEMA.INI
[Test_temp.csv]
ColNameHeader = false
Format = Delimited(;)
Col1 ="product"Text
Col2 ="price"Double

文本文件CSV:test01.txt
电视SAMSUNG 21" WIDESCREEN LED; 170
PHILIPS 27"WIDESCREEN LED; 200
HD SEAGATE 1TB 7200RPM; 150


代码VBA访问:

Sub TableImport()

    Dim strSQL As String
    Dim db As DAO.Database

    Dim strFolder As String
    strFolder = CurrentProject.Path

    Set db = CurrentDb

    strSQL = "DELETE FROM tbTest"
    db.Execute strSQL, dbFailOnError

    Dim strFile As String
    strFile = Dir(strFolder & "\test*.txt", vbNormal)

    Do Until strFile = ""

        FileCopy strFolder & "\" & strFile, strFolder & "\Test_temp.csv"

        strSQL = ""

        strSQL = " INSERT INTO tbTEST(product,price)"
        strSQL = strSQL & " SELECT fncReplace(product),price"
        strSQL = strSQL & " FROM [Text;HDR=no;FMT=Delimited;DATABASE=" & strFolder & "].Test_temp.csv"

        db.Execute strSQL, dbFailOnError

        strFile = Dir

    Loop

    db.Close

End Sub


Public Function fncReplace(varStr As Variant) As String
    If IsNull(varStr) Then
        fncReplace = ""
    Else
        fncReplace = Replace(Trim(varStr), """", "''")
    End If
End Function
Run Code Online (Sandbox Code Playgroud)


更新 - 它的工作 - 建议:Andre451

Sub TableImport()

    Dim strSQL As String
    Dim db As DAO.Database

    Dim strFolder As String
    strFolder = CurrentProject.Path

    Set db = CurrentDb

    strSQL = "DELETE FROM tbTest"
    db.Execute strSQL, dbFailOnError

    Dim strFile As String
    strFile = Dir(strFolder & "\test*.txt", vbNormal)

    Do Until strFile = ""

        FileCopy strFolder & "\" & strFile, strFolder & "\Test_temp.csv"

        DoCmd.TransferText acLinkDelim, "specIMPORTAR", "linkData", strFolder & "\Test_temp.csv", False

        strSQL = ""
        strSQL = " INSERT INTO tbTEST(product,price)"
        strSQL = strSQL & " SELECT product,price"
        strSQL = strSQL & " FROM linkData"

        db.Execute strSQL, dbFailOnError

        strFile = Dir

        DoCmd.DeleteObject acTable, "linkData"

    Loop

    db.Close

End Sub
Run Code Online (Sandbox Code Playgroud)

And*_*dre 1

读取 csv 文件时,双引号被解释为文本分隔符。在 SCHEMA.INI 中,似乎没有办法明确告诉 Access“没有文本分隔符!”。

所以我建议改用导入规范。您可以通过文本导入向导手动导入 csv 文件一次,然后将其保存为“产品导入规范”,从而创建导入规范。详情请参阅本答案中的1 .。

在规范中,您将“none”设置为文本分隔符。在德语访问中:

在此输入图像描述

然后链接文本文件并从中导入数据:

Public Sub ImportProducts()

    Dim S As String

    ' Link csv file as temp table
    DoCmd.TransferText acLinkDelim, "Product import specification", "linkData", "D:\temp\Test01.csv", False

    ' Insert from temp table into product table
    S = "INSERT INTO tbProduct (product, price) SELECT product, price FROM linkData"
    CurrentDb.Execute S

    ' Remove temp table
    DoCmd.DeleteObject acTable, "linkData"

End Sub
Run Code Online (Sandbox Code Playgroud)

编辑:

我创建了一个包含 1.000.000 行 (36 MB) 的 csv 文件,并将其用作导入文件:

Const cFile = "G:\test.csv"

Public Sub CreateCSV()

    Dim S As String
    Dim i As Long

    Open cFile For Output As #1
    For i = 1 To 1000000
        Print #1, "Testing string number " & CStr(i) & ";" & CStr(i)
    Next i
    Close #1

End Sub

Public Sub ImportProducts()

    Dim S As String
    Dim snTime As Single

    snTime = Timer

    ' Clean up product table
    CurrentDb.Execute "DELETE * FROM tbProduct"
    Debug.Print "DELETE: " & Timer - snTime

    ' Link csv file as temp table
    DoCmd.TransferText acLinkDelim, "Product import specification", "linkData", cFile, False
    Debug.Print "TransferText: " & Timer - snTime

    ' Insert from temp table into product table
    S = "INSERT INTO tbProduct (product, price) SELECT product, price FROM linkData"
    CurrentDb.Execute S
    Debug.Print "INSERT: " & Timer - snTime

    ' Remove temp table
    DoCmd.DeleteObject acTable, "linkData"

End Sub
Run Code Online (Sandbox Code Playgroud)

结果:

DELETE: 0
TransferText: 0,6640625
INSERT: 4,679688
Run Code Online (Sandbox Code Playgroud)

添加自动编号字段作为 tbProduct 的主键后:

TransferText: 0,6640625
INSERT: 8,023438
Run Code Online (Sandbox Code Playgroud)

8秒其实并不算慢。
确保 Access 数据库和导入的 CSV 文件都位于本地磁盘上,而不是网络驱动器上。如果可能的话,在 SSD 上。