+ Reply to Thread
Results 1 to 7 of 7

Inserting a function in an SQL query from VBA

  1. #1
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Inserting a function in an SQL query from VBA

    Hi guys,

    wonder if someone can help me on this...

    I am trying to create an SQL query in VBA to extra data based on various factors.

    I am having problems inserting the InStr() function in my WHERE clause, that will compare my table field CUST_ADDRESS.STATE with my entries made in sheet called SUMMARY range Z2:BL2 (where I list the actual states to find).... some of these may be blank and I want it to still work by ignoring blanks in that range...

    Can't figure out how to break up the InStr portion here so that it evaluates the function and returns the desired array.

    If you need more info, let me know.

    Please Login or Register  to view this content.
    Last edited by NBVC; 09-22-2010 at 04:28 PM.
    Where there is a will there are many ways.

    If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below left corner

    Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.

  2. #2
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Combining two formulas

    I just got it to accept the input into the VB editor as:

    Please Login or Register  to view this content.
    but still getting an error: Runtime Error 424, Object Required.

    This too gave be the above error... tried to isolate the CUST_ADDRESS.State:

    Please Login or Register  to view this content.
    and this one is giving type mismatch error:

    Please Login or Register  to view this content.
    Last edited by NBVC; 09-22-2010 at 11:50 AM.

  3. #3
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Inserting a function in an SQL query from VBA

    I think, maybe, InStr is not what I need here.. it, I guess works like the Search() function.. i.e. one string to one string.

    What I want to do, is like I described.. just instead of saying OR(Customer.state="AZ", Customer.state = "AL", etc... where Customer.state is a column of values... I want to know if the column contains any state in my defined range on my sheet....

    I think I should be using In (e.g. Where Customer.State In Range("Z2:BL2") ) but still not getting far....
    Last edited by NBVC; 09-22-2010 at 12:21 PM.

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

    Re: Inserting a function in an SQL query from VBA

    - Worksheets("Summary").Range("Z2:BL2") isn't a string, so instr() won't work.
    - the result of instr() is an integer, so in your case it doesn't test on being one of the values in the specified range.

    to turn the range into a string:

    c00=join(application.transpose(application.transpose(Worksheets("Summary").Range("Z2:BL2"))),"|")

    now you can use
    instr(CUST_ADDRESS.STATE,c00)>0

    althought I can't test it, it would turn out to:

    Please Login or Register  to view this content.

    sSql = sSql & "AND ((CUSTOMER_ORDER.ORDER_DATE>{ts '2009-12-31 00:00:00'}) AND (CUST_ORDER_LINE.LAST_SHIPPED_DATE Is Null) AND instr( CUST_ADDRESS.STATE,'" & c00 & "')>0) ORDER BY CUSTOMER_ORDER.CUSTOMER_ID "
    Last edited by snb; 09-22-2010 at 12:35 PM.



  5. #5
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Inserting a function in an SQL query from VBA

    That's now giving me a General ODBC error... # 1004.

    If I do a Debug.print after your c00=join.... nothing shows up in my immediate window.. but I get that error above.

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

    Re: Inserting a function in an SQL query from VBA

    Please Login or Register  to view this content.
    should at least show a result equal to string(38,"|") ; unless there is no such worksheet as "Summary" at all.

    but maybe the introduction of some apostrophs will do the trick

    sSql = sSql & "AND ((CUSTOMER_ORDER.ORDER_DATE>{ts '2009-12-31 00:00:00'}) AND (CUST_ORDER_LINE.LAST_SHIPPED_DATE Is Null) AND instr( CUST_ADDRESS.STATE,' " & chr(34) & c00 & chr(34) & " ')>0) ORDER BY CUSTOMER_ORDER.CUSTOMER_ID "

  7. #7
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Inserting a function in an SQL query from VBA

    Okay think I got with this:

    Please Login or Register  to view this content.
    seemed to be a missing closing parenthesis before the ORDER BY clause.

    I also figured it using the IN clause

    Please Login or Register  to view this content.
    Thanks very much.

+ 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