0

I can't seem to figure out what is wrong with my Connection String. I'm looking to query an Excel.CSV file.

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 <-- 'C:\Users\FirstName.LastName\Desktop\temp.csv' Not a valid path error
4
  • 2
    Since it's a CSV file, do not include full path in the path, try only "C:\Users\FirstName.LastName\Desktop\" Commented Jul 26, 2021 at 8:38
  • @Bharat How/where do i specify what file i want to query? Commented Jul 26, 2021 at 9:12
  • 1
    Filename is the table specified in the SELECT, for example "SELECT * FROM temp.csv;" Commented Jul 26, 2021 at 9:24
  • 1
    BTW you should consider using ACE instead of JET (better support for 64 bits environments) Commented Jul 26, 2021 at 9:26

1 Answer 1

1

Here is the full code for reference

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\"

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

    Debug.Print connStrng

    myConn.Open connStrng

    recSet .Open "SELECT * FROM temp.csv", myConn
    runSQLQueryForCSV = RecSet.GetRows()

    myConn.Close
    Set recSet = Nothing
    Set myConn = Nothing
End Function
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.