+ Reply to Thread
Results 1 to 3 of 3

ODBC Connection String with Multiple Variable "WHERE" requirements

  1. #1
    Registered User
    Join Date
    09-25-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    6

    ODBC Connection String with Multiple Variable "WHERE" requirements

    Hi,

    I am a little out of my element with this one:

    With VBA in Excel 2010 I am connecting to a SQL server by using ODBC.

    This is a two part process:

    Part one:
    I have created a connection string that gets me the following data: A, B, C, D, E from sheet tpoPurchOrder
    Where B is equal to "1"
    And
    Where C is equal to a changing field under Sheets("Macros").Range("B2")

    Here is part one
    Please Login or Register  to view this content.
    Part one works perfectly.

    Part two is where I have problems.
    Part two is a little different because I have the same connection, but what changes is the table that I am looking at "tpoPOLine" instead of "tpoPurchOrder" and the where is now going to have a variable number of commands.

    I want part two to be depend on part one. Under part one I want the result from column E to be a where statement in part two.

    As follows:

    In particular: *E3 from Part 1*, etc.

    Please Login or Register  to view this content.
    Now my problem is that sometimes the E column from part one ends up being 1 row, sometimes it ends up being 50 rows. I would like the code to change accordingly.

    My not so great solution:
    1. Pull everything from the SQL server filter once in excel. This is not as efficient as the database has ~300,000 rows in the tpoPOline table and would take a lot longer then needed.
    2. Create a nested if table and

    Change
    Please Login or Register  to view this content.
    to the value of that nested if table

    The nested if table would be something like =if(isblank(E2),"E1",if(isblank(E3)... etc for ~50 rows. I know the syntax is incorrect but you get my point.

    3. Rerun the query for each value in column E. That would require the connection to happen ~50 times which would not be that great, as well as I would have to copy and paste the data after each run as the tables cannot overlap.


    What I am looking for is a way to run this only once, on one sheet, without writing a nested if table with 50 if's.

    Any help would be great.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645
    Why not try using a subquery for the criteria in the 2nd part?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    09-25-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: ODBC Connection String with Multiple Variable "WHERE" requirements

    I did some reading with Subquerys and I came with the following:

    Please Login or Register  to view this content.
    I am getting a Syntax error.

    Is there a good source of information for nested Querys?

+ 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. Replies: 4
    Last Post: 07-12-2013, 12:14 PM
  2. If "String Variable" = "" then: (Fails)
    By vin1 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 02-17-2012, 07:11 AM
  3. use variable in Workbooks("book1").Worksheets("sheet1").Range("a1"
    By Luc in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-28-2005, 04:05 PM
  4. Replies: 3
    Last Post: 09-06-2005, 12:05 PM
  5. Replies: 0
    Last Post: 09-06-2005, 10:05 AM

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