+ Reply to Thread
Results 1 to 5 of 5

Inserting a formula in to SQL statement

  1. #1
    Forum Contributor
    Join Date
    08-29-2006
    Location
    London
    MS-Off Ver
    Excel 2016
    Posts
    314

    Inserting a formula in to SQL statement

    Dear all
    is there a way if we can insert a formula into SQL Command text? For example how can we insert this formula =Sum((A1:A25) + (B1:B25)) into excel query SQL Command text?


    kind reagsrd

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

    Re: Inserting a formula in to SQL statement

    This link: http://www.utteraccess.com/wiki/index.php/MS_Query has a discussion on how to use an excel table, the SUBSTITUTE command and some very short VB Code to rewrite the command string for a query.
    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.

  3. #3
    Forum Contributor
    Join Date
    08-29-2006
    Location
    London
    MS-Off Ver
    Excel 2016
    Posts
    314

    Re: Inserting a formula in to SQL statement

    thanks for your reply , I couldn't work this out

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Inserting a formula in to SQL statement

    No, there isn't. SQL and Excel work in completely different ways, you'd need to learn how to write the SQL for whatever it is you're trying to do

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

    Re: Inserting a formula in to SQL statement

    Maybe an example will help. Attached is a file. Pretend that you had created a very simple query with a connection name "MyConnectionName" that returns data to the Data sheet.

    Now you want to create a dynamic query to return data to that same sheet.

    On the control column sheet, I have data in columns E and F that need to be summed and used as a parameter in the query. For extra measure, I included a data that might also be part of the query.

    The sum of columns E and F are in Cell B1 on the Control Panel page.
    The date itself =TODAY()-1 (yesterday's date) is in Cell B2. However, this date is not in the format needed for a query. In fact it will show up as a date serial number such as 42422.
    So in Cell B3, I format the date into a format expected by the query.

    On the sheet SQL I have pseudo code in Column A. You will note that I have two "tokens" called &MyVariable and &Date (note that date is in single quotes - these are usually required by the SQL).

    All this information is in an Excel table called Table_SQL.

    In Column B I have the formula: =SUBSTITUTE(SUBSTITUTE([@[Raw SQL]],"&MyVariable",'Control Panel'!$B$1),"&Date",'Control Panel'!$B$3)

    What this does is takes the String &MyVariable and sets it to the value in Cell B1 of the Control Panel. It also converts &Date into the value contained in cell B3 of the Control Panel. Please note SUBSTITUTE is case sensitive.

    Cell B1 on the SQL page has the name SQLString. The formula is =SuperCat(Table_SQL[Translated SQL]). This is the new command string you want to use for the query.

    Then we have the following code in module SubSQL. All you have to do is run the macro Refresh Data, and it will substitute the new SQL for the old SQL and refresh the query.
    Please Login or Register  to view this content.
    Attached Files Attached Files

+ 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. [SOLVED] Inserting IF statement in to LARGE formula
    By amasson in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 06-03-2015, 10:08 AM
  2. Inserting a condition in a nested IF statement
    By oshodibo in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-27-2014, 10:56 AM
  3. Inserting an image using a nested if statement
    By RandyMPW in forum Hello..Introduce yourself
    Replies: 1
    Last Post: 02-21-2014, 08:05 PM
  4. [SOLVED] Inserting a chart using IF statement
    By Mortada in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-18-2013, 06:10 PM
  5. Inserting an extremely long IF statement.
    By Decoderman in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-17-2010, 10:13 AM
  6. Inserting a muliple IF statement in my recorded macro
    By prontrad in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-22-2009, 05:38 AM
  7. Inserting a block of cells using an if statement
    By fdaved in forum Excel General
    Replies: 10
    Last Post: 07-01-2008, 11:28 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