Chihiro
Excel Ninja
I've been playing with ADO as of late. And encountered an issue.
Normally when I use ADO to bring in data from closed workbook (.xlsx, .xlsm, .xlsb etc).
I use "IMEX=1;ImportMixedTypes=Text;" and it can bring in fields with mixed data types without loss of data.
However, when I use same options for CSV files. ADO seem to guess the data type and will end up with data loss. I have also tried various values for "typeguessrows" for ACE registry key with no difference in result.
Is there different option that can be set to work around this issue?
Currently working around it by converting csv to xlsx file before doing the query. But would like to know if there's another solution.
Code sample: (sample csv files in zip attached)
When you run it, you will see that third row is missing data (SKU = 1000C). As well as the first row (header).
Normally when I use ADO to bring in data from closed workbook (.xlsx, .xlsm, .xlsb etc).
I use "IMEX=1;ImportMixedTypes=Text;" and it can bring in fields with mixed data types without loss of data.
However, when I use same options for CSV files. ADO seem to guess the data type and will end up with data loss. I have also tried various values for "typeguessrows" for ACE registry key with no difference in result.
Is there different option that can be set to work around this issue?
Currently working around it by converting csv to xlsx file before doing the query. But would like to know if there's another solution.
Code sample: (sample csv files in zip attached)
When you run it, you will see that third row is missing data (SKU = 1000C). As well as the first row (header).
Code:
Sub UpdateSKU()
Dim ws As Worksheet
Dim fPath As String: fPath = "C:\Test\" 'Change as needed
Dim cn As Object, rst As Object
Set cn = CreateObject("ADODB.Connection")
With cn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=" & fPath & ";" & _
"Extended Properties=""text;HDR=NO;FMT=Delimited;Imex=1;ImportMixedTypes=Text;"""
.CursorLocation = 3
.Open
End With
strQuery = "SELECT t2.[F1], t1.[F2], t1.[F3], t1.[F4], t1.[F5] From [Test2.csv] as t1 " & _
"RIGHT OUTER JOIN [Test1.csv] as t2 " & _
" ON t2.[F1] = t1.[F1];"
Set rst = CreateObject("ADODB.Recordset")
rst.Open strQuery, cn, 1, 3
Range("A1").CopyFromRecordset rst
rst.Close
cn.Close
End Sub