Parse CSV, ignoring commas inside string literals in VBA?

后端 未结 11 1850
青春惊慌失措
青春惊慌失措 2020-12-30 03:27

I have a VBA application that runs every day. It checks a folder where CSVs are downloaded automatically, and adds their contents to a database. When parsing them, I reali

11条回答
  •  北海茫月
    2020-12-30 04:05

    If you are working with MS Access tables, there are advantages in simply importing text from disk. For example:

    ''If you have a reference to the Windows Script Host Object Model
    Dim fs As New FileSystemObject
    Dim ts As TextStream
    
    ''For late binding
    ''Dim fs As Object
    ''Dim ts As Object
    ''Set fs=CreateObject("Scripting.FileSystemObject")
    
    Set ts = fs.CreateTextFile("z:\docs\import.csv", True)
    
    sData = "1,2,3,""This should,be one part"",5,6,7"
    
    ts.Write sData
    ts.Close
    
    ''Just for testing, your table will already exist
    ''sSQL = "Create table Imports (f1 int, f2 int, f3 int, f4 text, " _
    ''     & "f5 int, f6 int, f7 int)"
    ''CurrentDb.Execute sSQL
    
    ''The fields will be called F1,F2 ... Fn in the text file
    sSQL = "INSERT INTO Imports SELECT * FROM " _
         & "[text;fmt=delimited;hdr=no;database=z:\docs\].[import.csv]"
    CurrentDb.Execute sSQL
    

提交回复
热议问题