Query CSV file using ADO - Run-time error '...is not a valid path' - help with connection string

OldNick

New Member
Joined
Jul 23, 2021
Messages
2
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hello there,

I have used ADO often to fetch Data from SQL Server and .xlsx Workbooks, but I can't seem to figure out what the problem with my connection string is here.

Error message : 'C:\Users\Firstname.Lastname\Desktop\temp.csv' is not a valid Path

VBA Code:
Sub test()

Dim myConn As New ADODB.Connection
Dim recSet As New ADODB.Recordset
Dim myPath As String
Dim connStrng As String
Dim qryStrng As String
Dim ReiterName As String

myPath = "C:\Users\Firstname.Lastname\Desktop\temp.csv"

connStrng = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
          "Data Source=" & myPath & ";" & _
          "Extended Properties=""text;HDR=YES;FMT=Delimited"""

Debug.Print connStrng

myConn.Open connStrng  '<--- Errror
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
I strongly suspect that your username folder is not really called Firstname.Lastname
If this is really part of your code this is probably why you get an error.
you can use this:
VBA Code:
myPath = Environ$("userprofile") & "\Desktop\temp.csv"
 
Upvote 0
you can use this:
VBA Code:
myPath = Environ$("userprofile") & "\Desktop\temp.csv"
Thanks for the reply, but no luck yet. I'm sure there's a typo in my connection string, but can't seem to figure out where/what
 
Upvote 0
It’s likely that last line of your connection string. I’ve run into issues when using multi-quotation marks when building connection strings, even when they look right in the immediate window.

I suggest you assemble the string as above but where you want quote marks use chr(34) instead.

VBA Code:
conStrng = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
          "Data Source=" & myPath & ";" & _
          "Extended Properties=" & chr(34) & “text;HDR=YES;FMT=Delimited" & chr(34)

The other thing that has sometimes tripped me up building connection strings is when I’ve copied the string from a website. Sometimes a quote mark copied from a website isn’t the same as one you’d type into the IDE. Thats probably not the case here, but thought I’d put it out there,
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top