+ Reply to Thread
Results 1 to 4 of 4

ADOdb not selecting correct data within Date Range

Hybrid View

  1. #1
    Registered User
    Join Date
    09-14-2012
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2010
    Posts
    17

    ADOdb not selecting correct data within Date Range

    I am using a ADOdb connection to import specific columns of data within a user-defined date range from 4 seperate tabs in another Excel file. For example: Start Date is selected by user. End Date is selected by user. File is selected by user. All data within the date range is pulled from "Granulation" tab, "Blending" tab, "Compression" tab, and "coating" tab. The error: Currently this macro is pulling rows of data outside of the date range and ignoring some data that is within the date range. It does not appear to be an issue with the source file...I have tried to copy/paste values into a seperate file to eliminate date format issues, but it still pulls data outside defined range. Error data frequently is associated only with dates beginning with 1 (January, October, November, December) despite selecting otherwise.

    Please review the macro and let me know if there are any issues OR a simpler way to achieve result.

    Thank you,
    Mark

    Date Range begin is entered in R2C1. Date Range end is entered in R2C2. Source file is entered in R2C4.

    Sub GetData()
     Const adStateOpen As Long = 1
     Const adOpenForwardOnly As Long = 1
     Const adLockReadOnly As Long = 1
     Const shOutput As String = "Output"
     Dim oCnn As Object
     Dim oRs As Object
     Dim oFields As Object
     Dim sFilePath As String
     Dim sSQL As String, sCnn As String
     Dim dStartDate As Date, dEndDate As Date
     Dim shArray As Variant
     Dim i As Long, Ptr As Long, LastRow As Long
    
    '   Check date formats
        With Worksheets("Report Setup")
            If Not IsDate(.Cells(2, "a")) Or Not IsDate(.Cells(2, "b")) Or Not Len(.Cells(2, "d")) > 0 Then
                MsgBox "Invalid date and or no file selected."
             Exit Sub
            End If
            dStartDate = .Cells(2, "a")
            dEndDate = .Cells(2, "b")
            sFilePath = .Cells(2, "d")
        End With
        
        sCnn = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" & sFilePath & ";Extended Properties=""Excel 12.0 Xml;HDR=YES"""
        
    '   Create Connection Object
        Set oCnn = CreateObject("ADODB.Connection")
    '   Open Created Connection
        On Error Resume Next
        oCnn.Open sCnn
    '   Check Connection State.
        If oCnn.State <> adStateOpen Then
            MsgBox "File Not found: " & vbCrLf & oCnn.ConnectionString, vbCritical
            Exit Sub
        End If
        On Error GoTo 0
        
        shArray = Array("Granulation", "Blending", "Compression", "Coating")
        For i = LBound(shArray) To UBound(shArray)
        '   Query sheet
            sSQL = "SELECT [Row #], [Room #], [Lot #], [Item #], [Product Description], [Batch Size], [Start Date], [Machine Hours] " & _
                   "FROM [" & shArray(i) & "$] " & _
                   "WHERE ((([Start Date])>=#" & dStartDate & "#) And " & _
                   "(([Start Date])<=#" & dEndDate & "#)) ORDER BY [Row #];"
        
            
            Set oRs = CreateObject("ADODB.Recordset")
        '   Open Created Recordset
            On Error Resume Next
            oRs.Open sSQL, oCnn
            If oRs.State <> adStateOpen Then
                MsgBox "Could not table: " & sSQL, vbCritical
                oCnn.Close
                Set oCnn = Nothing
                Exit Sub
            End If
            On Error GoTo 0
            
            Ptr = 0
            With Worksheets(shOutput)
                '.Cells.Clear
                If Not i > 0 Then
                    For Each oFields In oRs.Fields
                        Ptr = Ptr + 1
                        .Cells(1, Ptr) = oFields.Name
                    Next
                End If
                LastRow = .Cells(Rows.CountLarge, "a").End(xlUp).Row + 1
                If Not oRs.BOF Then
                    .Range("a" & LastRow).CopyFromRecordset oRs
                End If
            End With
        '   Close Recordset
            oRs.Close
            Set oRs = Nothing
        Next i
        With Worksheets(shOutput)
            'Convert text Date field and text Machine Hours field to number values:
            .Range("g2:g" & .Cells(Rows.CountLarge, "a").End(xlUp).Row) = _
               .Range("g2:g" & .Cells(Rows.CountLarge, "a").End(xlUp).Row).Value
            .Range("h2:h" & .Cells(Rows.CountLarge, "a").End(xlUp).Row) = _
               .Range("h2:h" & .Cells(Rows.CountLarge, "a").End(xlUp).Row).Value
        End With
    '   Close Connecton
        oCnn.Close
        Set oCnn = Nothing
        
    End Sub

  2. #2
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: ADOdb not selecting correct data within Date Range

    the last few lines seem to imply that your dates are stored as text and not dates which probably explains the behavior you see-the comparison is made on a textual basis and not a date one
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  3. #3
    Registered User
    Join Date
    09-14-2012
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: ADOdb not selecting correct data within Date Range

    I agree with you. But the cell Type = 1 (a value) on the source file. The actual contents of the cell is a vlookup on a date table. Those dates on the table are values as well.

    Does the connection create a recordset of text values only? Is there a way to prevent the recordset from converting date to text? Sorry for the non-expert description.

    Thanks for your help.
    Mark
    Last edited by mvgoggans; 10-15-2012 at 02:13 PM.

  4. #4
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: ADOdb not selecting correct data within Date Range

    do you have any cells in that column that do not return dates? I reckon something is causing Excel to treat the column as text - blank cells or formulas returning errors or "" could do that

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1