ADO To Excel

jstevens
GoldLounger
Posts: 2628
Joined: 26 Jan 2010, 16:31
Location: Southern California

ADO To Excel

Post by jstevens »

I'm having a challenge retrieving "text vs numeric" data using ADO.

Within a workbook named "MyFile.xls" there is a sheet named "Parameters" and it contains a series of text and numeric values in range B10:B20. The code (GetData_Range) works but has difficulty of returning numeric values as long as the entire range B10:B20 is used. Lets say that B10:B15 contains text while B16:B20 contains numeric values; only the text is returned.

If I change the range in the "GetData_Range" from B10:B20 to B16:B20 the numeric values are returned.

How does one return both text/numeric values in one pass using the entire range ie B10:B20?

Code: Select all

Sub GetData_Range()
Set af = Application.WorksheetFunction
strFileName = "MyFile.xls"
oPath = "C:\Temp\"

' It will copy the Header row also (the last two arguments are True)
' Change the last argument to False if you not want to copy the header row
    
        GetData oPath & strFileName, "Parameters", _
                "B10:B20", Sheets("Source.RngNames").Range("B1"), False, False

End Sub


Public Sub GetData(SourceFile As Variant, SourceSheet As String, _
                   SourceRange As String, TargetRange As Range, Header As Boolean, UseHeaderRow As Boolean)
'Working in Excel 2000-2007
    Dim rsCon As Object
    Dim rsData As Object
    Dim szConnect As String
    Dim szSQL As String
    Dim lCount As Long

    ' Create the connection string.
    If Header = False Then
        If Val(Application.Version) < 12 Then
            szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                        "Data Source=" & SourceFile & ";" & _
                        "Extended Properties=""Excel 8.0;HDR=No"";"
        Else
            szConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                        "Data Source=" & SourceFile & ";" & _
                        "Extended Properties=""Excel 12.0;HDR=No"";"
        End If
    Else
        If Val(Application.Version) < 12 Then
            szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                        "Data Source=" & SourceFile & ";" & _
                        "Extended Properties=""Excel 8.0;HDR=Yes"";"
        Else
            szConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                        "Data Source=" & SourceFile & ";" & _
                        "Extended Properties=""Excel 12.0;HDR=Yes"";"
        End If
    End If

    If SourceSheet = "" Then
        ' workbook level name
        szSQL = "SELECT * FROM " & SourceRange$ & ";"
    Else
        ' worksheet level name or range
        szSQL = "SELECT * FROM [" & SourceSheet$ & "$" & SourceRange$ & "];"
    End If

    On Error GoTo SomethingWrong

    Set rsCon = CreateObject("ADODB.Connection")
    Set rsData = CreateObject("ADODB.Recordset")

    rsCon.Open szConnect
    rsData.Open szSQL, rsCon, 0, 1, 1

    ' Check to make sure we received data and copy the data
    If Not rsData.EOF Then

        If Header = False Then
            TargetRange.Cells(1, 1).CopyFromRecordset rsData
        Else
            'Add the header cell in each column if the last argument is True
            If UseHeaderRow Then
                For lCount = 0 To rsData.Fields.Count - 1
                    TargetRange.Cells(1, 1 + lCount).Value = _
                    rsData.Fields(lCount).Name
                Next lCount
                TargetRange.Cells(2, 1).CopyFromRecordset rsData
            Else
                TargetRange.Cells(1, 1).CopyFromRecordset rsData
            End If
        End If

    Else
        MsgBox "No records returned from : " & SourceFile, vbCritical
    End If

    ' Clean up Recordset object.
    rsData.Close
    Set rsData = Nothing
    rsCon.Close
    Set rsCon = Nothing
    Exit Sub

SomethingWrong:
    MsgBox Err.Number & " " & Err.Description

    MsgBox "The file name, Sheet name or Range is invalid of : " & SourceFile, _
           vbExclamation, "Error"
    On Error GoTo 0

End Sub

Thanks,
John
Regards,
John

User avatar
HansV
Administrator
Posts: 78486
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: ADO To Excel

Post by HansV »

GetData uses the CopyFromRecordset method which is intended for databases. In a database recordset, all data in a column are of the same type.
So you should ensure that all data in the range B10:B20 on the Parameter sheet are text, for example by inserting apostrophes in front of the number values in B16:B20, and formatting the column as text.
Best wishes,
Hans

User avatar
rory
5StarLounger
Posts: 817
Joined: 24 Jan 2010, 15:56

Re: ADO To Excel

Post by rory »

You need to add IMEX=1 to the extended properties in the connection string.
Regards,
Rory

jstevens
GoldLounger
Posts: 2628
Joined: 26 Jan 2010, 16:31
Location: Southern California

Re: ADO To Excel

Post by jstevens »

Hi Rory,

Thanks for the suggestion. I tried it and have encountered the same result where the numeric values are not being picked up.

Regards,
John
Regards,
John

User avatar
HansV
Administrator
Posts: 78486
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: ADO To Excel

Post by HansV »

Do you have a particular reason for wanting to use this method? You could open the workbook and copy/paste the cells.
Best wishes,
Hans

User avatar
rory
5StarLounger
Posts: 817
Joined: 24 Jan 2010, 15:56

Re: ADO To Excel

Post by rory »

How exactly did you use it?
Regards,
Rory

jstevens
GoldLounger
Posts: 2628
Joined: 26 Jan 2010, 16:31
Location: Southern California

Re: ADO To Excel

Post by jstevens »

Hans - I orginally planned on using the code to extract "text/values" from a RangeName in a closed workbook. The code is flexible either to use a
RangeName or a specific range such as "B10:B20". My intent was never to open the workbook or write a formula to a specific cell in the closed
workbook and range value it.

One of the things I have learned using ADO is that I can include a header: this is optional via True/False. It truly has it's merits.

Rory - I create a list of files along with specific text/values associated within each file. Think of this as a validity check list.

I have created a work around where I break down "B10:B20" to smaller ranges such as "B10:B12", "B13:B15" and so forth.

It would have been great just to reference the entire range "B10:B20" once.

Regards,
John
Regards,
John

User avatar
rory
5StarLounger
Posts: 817
Joined: 24 Jan 2010, 15:56

Re: ADO To Excel

Post by rory »

That doesn't answer my question. I meant how exactly did you use the IMEX=1 option?
Regards,
Rory

jstevens
GoldLounger
Posts: 2628
Joined: 26 Jan 2010, 16:31
Location: Southern California

Re: ADO To Excel

Post by jstevens »

Rory,

This is how I'm using it. Low and behold, I tried it again this morning and it worked.

Code: Select all

          szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                        "Data Source=" & SourceFile & ";" & _
                        "Extended Properties=""Excel 8.0;HDR=No;IMEX=1"";"
I must have done something wrong yesterday.

Thanks again for the suggestion...You too Hans! :thankyou:

Regards,
John
Regards,
John