+ Reply to Thread
Results 1 to 2 of 2

Loop through range of cells and return price in SQL query

  1. #1
    Registered User
    Join Date
    11-18-2016
    Location
    Stockholm
    MS-Off Ver
    2016
    Posts
    1

    Loop through range of cells and return price in SQL query

    Hi,

    I use the following code to search for article numbers in a textfield on a form, in a sql database and return the price.
    But what If I would like to search for serveral article numbers at the same time from a range of cells on the active sheet and return all prices and print them on the same row as each article number but in a different column?

    Would much appreciate the help!

    Please Login or Register  to view this content.
    Last edited by Rödtjut; 01-20-2017 at 02:39 PM.

  2. #2
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    Re: Loop through range of cells and return price in SQL query

    This seems to be more of a SQL statement, However, the attached shows a way to write the SQL dynamically once you get it figured out. I suggest returning the data to a "normalized form" of the format Article Number | Price and use a Pivot Table to format and collect the results.

    Maybe this program can help you. It outlines a means for making SQL “on the fly.

    I use this program to develop a list of models to track in another report. For example, there are numerous SKUs that roll up into the iPhone 7.

    On the Model List sheet, I fill out column A and B and column C has a formula to produce the code snippet I want to use in my SQL statement.

    The results of this query are on the Model D tab. I “zeroed” out some of the information to make the data non-sensitive. This table gets copied to a CSV file for use by other programs.

    The part of interest to you is the SQL sheet. It contains three tables: Table_SQL, Table_SQL_Head and Table_SQL_End.

    Table_SQL_Head and Table_SQL_End are static.

    What the code does is clears out Table_SQL, copies in Table_SQL_Head, then appends the code snippets from the Model List and then appends Table_SQL_End.

    Part of the code is a UDF called SuperCat which takes Table_SQL and makes it one big string. This string has a name, SQL_Model_D and is in cell A1 on the SQL Sheet. This is what you can set your SQLStr to in your code.

    ModRefreshData has the main code. You will probably want to drop the part about writing to the CSV file. Also change the part that runs the query (SubSQL). Call your code here instead, just remember to set your SQL string to SQL_Model_D (or whatever you choose to call it).

    You will also have to change the formulas in the table on the Model List sheet to produce the code snippets to meet your needs. In this case I am producing a bunch of case statements. In other programs I produce “IN” statements.

    You will also have to change the contents of Table_SQL_Head and Table_SQL_End to reflect your query.

    FYI: Cell E17 on the SQL sheet shows how to pass a parameter to the SQL code.
    Attached Files Attached Files
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

+ 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: 3
    Last Post: 08-27-2014, 05:05 PM
  2. [SOLVED] Lookup Code and return Price from Price Band
    By PaulHAG in forum Excel Formulas & Functions
    Replies: 15
    Last Post: 06-16-2014, 11:06 AM
  3. [SOLVED] Loop over all queries and return first column of each query in consolidated table
    By learning_vba in forum Access Tables & Databases
    Replies: 2
    Last Post: 01-03-2014, 10:19 AM
  4. Replies: 1
    Last Post: 07-24-2013, 12:13 PM
  5. [SOLVED] Query Oracle using range in Excel to return individual records for each cell in range
    By bigwillydier in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-18-2013, 06:37 PM
  6. Replies: 1
    Last Post: 07-03-2011, 05:12 PM
  7. Replies: 8
    Last Post: 08-06-2010, 07:27 AM

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