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;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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With