Results 1 to 2 of 2

Data Connections > SQL Query > Use Excel Range to filter

Threaded View

  1. #2
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Data Connections > SQL Query > Use Excel Range to filter

    So - you want to dynamically add a WHERE clause to your SQL statement based on the sheet? Is that correct?


    Edit:

    NOTE: This will only work if you edit my code to fit your project This is the general technique I've used in the past for this same situation. The key is, you can access the Command Text of your query table programmatically.


    It looks to me that you are looking to add a clause like: WHERE Name IN ('Tom','Harry','Jane'). To your SQL statement.


    The way I do this, is to use VBA to re-author the query as needed.

    Your query sits in a table object in the workbook, and it has a specific name -- assume that name is T_People. You need a macro to alter the command text as needed.

    Something like:

    Sub alterCMDText()
    Dim S As String
    Dim namesRange As Range, c As Range
    
    
    Set namesRange = Sheet1.Cells("A1:A10")
    
    S = "Select * from row WHERE YEAR([Date]) = 2015 OR (YEAR([Date]) = 2014 AND MONTH([Date]) = 12 OR MONTH([Date]) = 11)"
    S = S & " AND name IN ('"
    
    'Add Names'
    For Each c In nameRange
     S = S & c.Value & "','"
    Next c
    
    'End the IN list properly'
    S = Left(S, Len(S) - 2) & ") "
    
    'Add rest of SQL'
    S = S & " order by ID asc"
    
    Sheet1.ListObjects("T_People").QueryTable.CommandText = S
    Sheet1.ListObjects("T_People").QueryTable.Refresh (False)
    
    End Sub
    Then you just need to decide what launches this (a button? a worksheet event? a workbook event?)...
    Last edited by GeneralDisarray; 10-22-2015 at 11:05 AM.
    Remember, saying thanks only takes a second or two. Click the star icon(*) below the post you liked, to give some Rep if you think an answer deserves it.

    Please,mark your thread [SOLVED] if you received your answer.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. SQL connections, wait with the code until query is done!
    By Reemet in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-14-2015, 02:38 AM
  2. Dynamic source data in QUERY connections
    By Glenn Kennedy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-21-2013, 06:16 AM
  3. Excel - web query error with some ISP Wifi connections
    By giaco in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-05-2013, 04:21 AM
  4. [SOLVED] Data Connections: Parameterized Query of Access DB... Is it possible?
    By twointum in forum Excel General
    Replies: 4
    Last Post: 06-27-2012, 01:25 PM
  5. Replies: 2
    Last Post: 05-24-2012, 08:43 AM
  6. Run Away Data Connections / Query?
    By mrIslic in forum Excel General
    Replies: 0
    Last Post: 10-04-2011, 10:18 AM
  7. Connections to Access DB - query availability
    By jamsta1972 in forum Excel General
    Replies: 1
    Last Post: 01-04-2011, 06:20 PM

Tags for this Thread

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