.csv的ADODB连接string

我想在Excel VBA中使用ADODB处理.csv文件。 我尝试了几个在网上find的string,但他们都没有工作。 我正在使用以下文件path:

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

然后我将strVFile作为parameter passing给子objReport.Load strVFile 。 子的标头是: Public Sub Load(ByVal strFilename As String)

然后我尝试使用string进行ADODB连接:

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

当我运行macros并selectCSV文件时,有错误发生,说“给定的path不是一个有效的path”。 我究竟做错了什么?

编辑(代码)

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;Data Source=" & strFilename & _ ";Extended Properties=""text;HDR=Yes;FMT=Delimited(;)"";Persist Security Info=False" pconConnection.Open End Sub 

对于文本文件, Data Source是文件夹,而不是文件。 该文件是表(SELECT * FROM ..)。 请参阅http://www.connectionstrings.com/textfile

我find了我的问题的答案。 对于文本文件(如Remou所述), Data Source只是文件夹path,没有文件名。 另外,不要使用:

 C:\dir\dir2\ 

我不得不使用

 C:\\dir\\dir2\\ 

从完整path获取文件名称:

 strFilename = Dir(strFilepath) 

仅获取path,不包含文件名:

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

要将path格式从“\”更改为“\\”,我只是使用:

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

问题解决了,感兴趣。