I aim to get data from a csv file using an SQL request. I already made it successfully in a previous Workbook, so I copied it and modified it only on the SQL request part. I have an execution error 9 on the first line, which has not been modified after copying the code:
ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array( _
"ODBC;DBQ=" & Path & ";DefaultDir=" & Path & _
";Driver={Microsoft Access Text Driver (*.txt, *.csv)};DriverId=27;FIL=text", _
";MaxBufferSize=2048;MaxScanRows=8;PageTimeout=5;SafeTransactions=0;Threads=3;UID=admin;UserCommitSync=Yes;" _
), Destination:=ThisWorkbook.Sheets("Sessions").Range("$A$1")).QueryTable
The variable Path is the path to get to the folder where the file is stored. The Sheet "Sessions" is created before this line is ran, an is activated.
This is the complete code of the request:
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array( _
"ODBC;DBQ=" & Path & ";DefaultDir=" & Path & _
";Driver={Microsoft Access Text Driver (*.txt, *.csv)};DriverId=27;FIL=text", _
";MaxBufferSize=2048;MaxScanRows=8;PageTimeout=5;SafeTransactions=0;Threads=3;UID=admin;UserCommitSync=Yes;" _
), Destination:=ThisWorkbook.Sheets("Sessions").Range("$A$1")).QueryTable
.CommandText = Array( _
Selection & Chr(13) & Chr(10) & "FROM `" & NameFile(1) & "` `" & NameFile(2) & "`" & _
Chr(13) & Chr(10) & Condition)
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Session"
.Refresh BackgroundQuery:=False
End With
I am quite new with connections in VBA, as it is only my second program using it. It is probably very simple to correct but I did not find a working solution for my case.