+ Reply to Thread
Results 1 to 2 of 2

Data Connections > SQL Query > Use Excel Range to filter

  1. #1
    Registered User
    Join Date
    02-09-2008
    Posts
    32

    Data Connections > SQL Query > Use Excel Range to filter

    Hi Everyone,

    Apologies in advance if my terminology is poor at any point.
    This might explain why my Google searches for the answer have proved fruitless.

    My Excel doc is pointing to an Access database for its data connection.
    I have a simple SQL query running in this data connection...

    Select * from row WHERE YEAR([Date]) = 2015 OR (YEAR([Date]) = 2014 AND MONTH([Date]) = 12 OR MONTH([Date]) = 11) order by ID asc

    Now ideally what i would like to do is use cells in my Excel spread sheet instead of hard coding them into the SQL query.

    My Access database has the headers:
    Name Date TimeSpent Reason Allowance Supplier AllowanceDate Activity Tier AdsCompleted Country QuoteNo Comments ID

    I would like to only return the data for entries in the database where the name appears in a range of cells.

    So the SQL brings back any data relating to names in a certain cell range.

    A B
    1 Tom Good
    2 **** Bad
    3 Harry Ugly

    Does that all make sense?

  2. #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:

    Please Login or Register  to view this content.
    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.

+ 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. 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