+ Reply to Thread
Results 1 to 14 of 14

UDF SQL Statement help

  1. #1
    Forum Contributor
    Join Date
    01-18-2010
    Location
    Bentley, NM
    MS-Off Ver
    Excel 2010
    Posts
    229

    UDF SQL Statement help

    I have created a UDF that uses an ODBC connection to return Sales Units based on a set of criteria: Line, Item#, BeginDate, EndDate, Price.

    Currently it sums all SalesUnits within the given period based on the price. However, I would like for it to subtract SalesReturns in order to get a more accurate number and avoiding writing a new function that gets returns only (the sales one takes over an hour as is).

    Code for current function:

    Please Login or Register  to view this content.
    I have tried replacing
    Please Login or Register  to view this content.
    with
    Please Login or Register  to view this content.
    and several variations of that with parentheses in different spots, but have had no luck. Can I achieve this calculation within one VBA function?

  2. #2
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: UDF SQL Statement help

    What is JDQTYS?

  3. #3
    Forum Contributor
    Join Date
    01-18-2010
    Location
    Bentley, NM
    MS-Off Ver
    Excel 2010
    Posts
    229

    Re: UDF SQL Statement help

    Sorry, that is Sales Units.
    I am trying to get QTYS - QTYR (Units-Returns).

  4. #4
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: UDF SQL Statement help

    I mean is it a table column, a VBA variable, where does it come from?

  5. #5
    Forum Contributor
    Join Date
    01-18-2010
    Location
    Bentley, NM
    MS-Off Ver
    Excel 2010
    Posts
    229

    Re: UDF SQL Statement help

    It is a field that comes from DATAWHSE.DWPOSDATA.JDXMIT which is a table linked to Excel through ODBC.

  6. #6
    Forum Contributor
    Join Date
    01-18-2010
    Location
    Bentley, NM
    MS-Off Ver
    Excel 2010
    Posts
    229

    Re: UDF SQL Statement help

    Still trying to make a calculation within my VBA SQL statement.

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,015

    Re: UDF SQL Statement help

    Wouldn't it just be:
    Please Login or Register  to view this content.
    ?
    Everyone who confuses correlation and causation ends up dead.

  8. #8
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: UDF SQL Statement help

    Using the ODBC connection you can import all the relevant data for the calculation.
    Instead of using a function that is importing line by line, reading line by line and writing line by line I think you'd better use a macro in the importing worksheet. Reading the relevant values into an array, performing the calculation in that array and writing into that array.
    At the end you can write the array into the 'importworksheet'.
    I fear it will improve the performance considerably.

    Example:
    Please Login or Register  to view this content.



  9. #9
    Forum Contributor
    Join Date
    01-18-2010
    Location
    Bentley, NM
    MS-Off Ver
    Excel 2010
    Posts
    229

    Re: UDF SQL Statement help

    romper,
    I would think so, but I cannot get it to work this way so far. It just returns a blank cell.

    snb,
    Thanks for the suggestion. It looks a little steep for my skill level, but I would like to understand it as it could be able to help with the performance of many current functions that I use.

    Reading the relevant values into an array
    By relevant values do you mean only the values brought back by the criteria? The table I am pulling from is massive and slow, so I would have to only bring in the values I need.

    I have never used an array before either, so your code is a little confusing to me..
    Last edited by maw230; 11-15-2010 at 11:01 AM.

  10. #10
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,015

    Re: UDF SQL Statement help

    Are both the fields numeric, and do they both contain data (rather than maybe being null)?

  11. #11
    Forum Contributor
    Join Date
    01-18-2010
    Location
    Bentley, NM
    MS-Off Ver
    Excel 2010
    Posts
    229

    Re: UDF SQL Statement help

    Yes, both fields are numbers. Quantity Sold minus Quantity Returned.

  12. #12
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,015

    Re: UDF SQL Statement help

    Is either field ever null? What is the database?

  13. #13
    Forum Contributor
    Join Date
    01-18-2010
    Location
    Bentley, NM
    MS-Off Ver
    Excel 2010
    Posts
    229

    Re: UDF SQL Statement help

    No, never nulls. There would be 0's.
    What is the database?
    Not sure I know what you mean. The table are stored on IBM AS/400 backend..

  14. #14
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,015

    Re: UDF SQL Statement help

    Have you checked to see if an error is being raised (you don't currently do anything in your error handler)?
    I'm also not sure why you are returning a string here?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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