0

I am using the function below to extract data from another workbook. It works for non-Date columns, but does not work for a column which holds dates

Worksheet looks like

enter image description here

Works if is not a date column:

Debug.Print GetSheetSQL(workbookPath, "Select columna from [sheet$]")(1)("columna")

--> Returns "palim"

Does not work if a is a date column

Debug.Print GetSheetSQL(workbookPath, "Select columnb from [sheet$]")(1)("columnb")

--> Returns Nothing

Function GetSheetSQL(path As String, sqlStr As String) As Collection

'''''''''''''''''''''''''''''''''''''''
'Open ADOB Connection and query via sql
'   Connection string is standard and
'   taken from:
'        https://www.connectionstrings.com/microsoft-jet-ole-db-4-0/standard-excel/

Dim objConnection As Object
Dim objRecordSet As Object
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")

Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H1

objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & path & ";Extended Properties=""Excel 8.0;HDR=Yes"""

objRecordSet.Open sqlStr, objConnection, adOpenStatic, adLockOptimistic, adCmdText
'''''''''''''''''''''''''''''''''''''''
'Iterate through queried table
'   table is a collection with a row per row in the table
'   row is a dictionary with table headings as key, returning the corresponding value
Dim table As Collection
Set table = New Collection
Dim row As Object
Dim fld
'iterate through recordset rows
Do Until objRecordSet.EOF
    With objRecordSet
        Set row = CreateObject("Scripting.Dictionary")

        For Each fld In .Fields
            row.Add fld.Name, fld.Value
        Next fld

        table.Add row

        .MoveNext

    End With
Loop

Set GetSheetSQL = table

'Close Connection; reset Error Handling to default
objConnection.Close
On Error GoTo 0
Exit Function
ErrorCloseConn:
    objConnection.Close
    On Error GoTo 0
    Resume
End Function
9
  • I tried your code and it worked fine for me. What do you mean with Returns Nothing? Commented Aug 1, 2018 at 17:08
  • Your SQL statement is missing a few quotation marks -- "Select ... [sheet$]" Commented Aug 1, 2018 at 17:11
  • Sorry I do have those in the original stmt. I'll change that Commented Aug 1, 2018 at 17:14
  • @Storax it returns the string "NULL" although I am sure there is sth in that row or sometimes just "" Commented Aug 1, 2018 at 17:15
  • I could reproduce your problem. Is it possible that one of your date fields is actually text. At least that's the way I was able to reproduce the issue. In this case ADODB will just ignore the content of the cell in excel. Commented Aug 1, 2018 at 17:16

1 Answer 1

2

I was able to re-produce the issue, maybe this is of help

enter image description here

This looks fine but I changed the first cell with a date to text. Debugging the code will give you

enter image description here

So, ADODB expects a date, Type is adDate and converts text to an empty string.

The following update solved the problem in the end

UPDATE It might help to change the connection string to

objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & path & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""

For further information look here

Sign up to request clarification or add additional context in comments.

9 Comments

Does this affect all the rows? Or only the row with the text data in it?
For me it says adVarWChar. I formatted all cells to text, but the issue persists. I do have some empty cells in the date column. Could that be the issue? Thanks a lot for helping me!!!
I also formatted all cells to text then I also get the data type adVarWChar but I also get a result
Then the issue is somewhere with my data :/
Didn't see your update but ended up with the same solution IMEX=1 solves all my issues, no need to format or anything. Cheers man!
|

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.