+ Reply to Thread
Results 1 to 5 of 5

Trying to Query between two dates

  1. #1
    Forum Contributor
    Join Date
    05-14-2012
    Location
    USA
    MS-Off Ver
    Excel 2013
    Posts
    186

    Trying to Query between two dates

    So I have inherited a workbook that contains a broken macro that I can't figure out how to fix and I'm hoping the forums can come through for me and help me out of a bind...again

    Here is the code in the macro :

    Sub RefreshQuery()
    '
    ' RefreshQuery Macro
    '
    Dim queryPreText As String
    Dim queryPostText As String
    Dim valueToFilter As String
    Dim paramPosition As Integer
    valueToFilter = "[GL_Posting_Date] <="

    With ActiveWorkbook.Connections("XXXX dbo view_AR_Apply_Detail").OLEDBConnection
    queryPreText = .CommandText
    paramPosition = InStr(queryPreText, valueToFilter) + Len(valueToFilter) - 1
    queryPreText = Left(queryPreText, paramPosition)
    queryPostText = .CommandText
    queryPostText = Right(queryPostText, Len(queryPostText) - paramPosition)
    queryPostText = Right(queryPostText, Len(queryPostText) - InStr(queryPostText, ")") + 1)
    .CommandText = queryPreText & " '" & Range("N3").Value & "'" & queryPostText
    End With
    ActiveWorkbook.Connections("XXXX dbo view_AR_Apply_Detail").Refresh

    With ActiveWorkbook.Connections("PGLMD dbo view_AR_Apply_Detail").OLEDBConnection
    queryPreText = .CommandText
    paramPosition = InStr(queryPreText, valueToFilter) + Len(valueToFilter) - 1
    queryPreText = Left(queryPreText, paramPosition)
    queryPostText = .CommandText
    queryPostText = Right(queryPostText, Len(queryPostText) - paramPosition)
    queryPostText = Right(queryPostText, Len(queryPostText) - InStr(queryPostText, ")") + 1)
    .CommandText = queryPreText & " '" & Range("N3").Value & "'" & queryPostText
    End With
    ActiveWorkbook.Connections("YY dbo view_AR_Apply_Detail").Refresh

    With ActiveWorkbook.Connections("YY dbo view_AR_Apply_Detail").OLEDBConnection
    queryPreText = .CommandText
    paramPosition = InStr(queryPreText, valueToFilter) + Len(valueToFilter) - 1
    queryPreText = Left(queryPreText, paramPosition)
    queryPostText = .CommandText
    queryPostText = Right(queryPostText, Len(queryPostText) - paramPosition)
    queryPostText = Right(queryPostText, Len(queryPostText) - InStr(queryPostText, ")") + 1)
    .CommandText = queryPreText & " '" & Range("N3").Value & "'" & queryPostText
    End With
    ActiveWorkbook.Connections("YY dbo view_AR_Apply_Detail").Refresh

    End Sub

    -----

    N2 is the first date and N3 is the last date that I am looking for information for. I can't get it to run between them. Any idea how to fix the macro? The person before me got it this far but couldn't make it work right.

    Any help is much appreciated.

    Thanks again

  2. #2
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: Trying to Query between two dates

    Whats the error messages etc? if you can then debug to see what line(s) are causing the issue that would help.

    ps You'll need to put your code inside [code] tags, highlight the Sub and click the # option in the editor.

  3. #3
    Forum Contributor
    Join Date
    05-14-2012
    Location
    USA
    MS-Off Ver
    Excel 2013
    Posts
    186

    Re: Trying to Query between two dates

    So it is running, it just isn't running as intended. The book is supposed to pull entries between 2 dates but is instead pulling all year to date data. The questions is more academic than "can you fix the debug error". I know what they were trying, and hat they couldn't get it to work. I am just learning the basics of writing my own macros from scratch and am stuck scratching my head here.

  4. #4
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: Trying to Query between two dates

    Chances are the problem isnt a code one but a difference between what the code expects and what data it is using, without seeing your workbook its hard to see what happens. Id recommend running it as a debug and stepping through looking at the values at each step to see why its making the decisions it is, chances are theres a rogue variable name or something which can only be spotted doing this.

  5. #5
    Forum Contributor
    Join Date
    05-14-2012
    Location
    USA
    MS-Off Ver
    Excel 2013
    Posts
    186

    Re: Trying to Query between two dates

    ok, thanks, I'll give it a shot.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Help with web query dates please.
    By enzihsehtsilecxe in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-23-2012, 10:34 PM
  2. Excel 2007 : =IF formula help/query for dates.
    By cayal in forum Excel General
    Replies: 3
    Last Post: 09-02-2010, 09:58 PM
  3. IF function query on Day/Dates
    By quickshot in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-01-2007, 01:48 AM
  4. web query in VBA (format dates in url)
    By slimswol in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-11-2006, 05:25 AM
  5. [SOLVED] dates in query
    By patrick @fizz in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 04-08-2005, 03:06 PM

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