开发者

ADODB Connection String for .csv

I want to process .csv files with ADODB in Excel VBA. I tried a few strings found on web, but none of them seems to work. I'm getting file path using:

strVFile = Application.GetOpenFilename("CSV (*.csv), *.csv")

And then I pass strVFile as a parameter to the sub objReport.Load strVFile. The header of the sub is: Public Sub Load(ByVal strFilename As String).

Then I try to make ADODB connection using string:

pconConnection.ConnectionString = _
            "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFilename & _
            ";Extended Properties=""text;HDR=Yes;FMT=Delimited(;)"";Persist Security Info=False"
    pconConnection.Open

When I run the macro and choose CSV file, there's error occuring saying that "given path is not a valid path". What am I doing wrong?

Edit (Code),

Module mdlReport

Public Sub Report()
    Dim objReport As clsReport


    MsgBox "Please select .csv file", vbInformation + vbOKOnly
    strVFile = Application.GetOpenFilename("CSV (*.csv), *.csv")

    If strVFile <> False Then
        Set objReport = New clsReport

        objReport.Load strVFile

    End If
End Sub

Class clsReport

Private pconConnection As ADODB.Connection
Private prstRecordset As ADODB.Recordset

Private Sub Class_Initialize()
  Set pconConnection = New ADODB.Connection
  pconConnection.ConnectionTimeout = 40
End Sub

Public Sub Load(ByVal strFilename As String)

    pconConnection.ConnectionString = _
            "Provider=Microsoft.Jet.OLEDB.4.0开发者_Go百科;Data Source=" & strFilename & _
            ";Extended Properties=""text;HDR=Yes;FMT=Delimited(;)"";Persist Security Info=False"
    pconConnection.Open

End Sub


For a text file, Data Source is the folder, not the file. The file is the table (SELECT * FROM ..). See http://www.connectionstrings.com/textfile


Here is an update using Microsoft.ACE.OLEDB.16.0 as provider.

Make sure the reference library "Microsoft ActiveX Data Objects 6.1 Library" is added to VBAproject first.

Sub testrunSQLQueryForCSV()
    Dim arrayTest
    arrayTest = runSQLQueryForCSV("C:\xxx\yyyy\", "SELECT * FROM mycsvfile.csv")

    'NOTE: for CSV the Data Source reference is to the directory and the csv file is similar
    'to one "worksheet" in an excel file and is simply referenced in the SQL statement
End Sub


Public Function runSQLQueryForCSV(fileDirPath As String, SQLStatement As String)

    Dim Conn As New ADODB.Connection
    Dim RecSet As New ADODB.Recordset

    With Conn
       .Provider = "Microsoft.ACE.OLEDB.16.0"  'Can use many providers, but this is the latest and it works with csv files also
       .ConnectionString = "Data Source=" & fileDirPath & ";Extended Properties='text'"
    End With

    Conn.Open

    RecSet.Open SQLStatement, Conn
    runSQLQueryForCSV = RecSet.GetRows()

    Conn.Close
    Set RecSet = Nothing
    Set Conn = Nothing
End Function


I found the answer to my problem. For text files (as stated by Remou) Data Source is just the folder path, without file name. In addition instead of using:

C:\dir\dir2\

I had to use

C:\\dir\\dir2\\

To get file name from full path:

strFilename = Dir(strFilepath)

To get the path only, without a file name:

strFilepath = Left$(strFilepath, InStrRev(strFilepath, "\"))

To change path format from '\' to '\\' I just used:

strFilepath = Replace(strFilepath, "\", "\\")

The problem is solved, thanks for interest.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜