Fix "Run-Time error 13: Type MisMatch" in sql query with complex "Where" statement

  • This is my first time asking a question in forum so be gentle.


    As background, the sql (admittedly un-elegant) query vba code below has been used for months and previously tested to work. Suddenly it doesn't and throws the error noted in the title to this question when VBA execution hits the sql query line of code.


    Troubleshooting reveals that the length of the character string stored in variable 'CriteriaString' seems to be what causes the error. Seems like if the length of the string stored in 'CriteriaString', gets over about 150, I get the noted error.


    Code Overview: This code pulls data from an inventory server database. You will note a userform is presented where the user selects specific values to be searched for in up to three different fields. The user form contains check boxes so multiple values can be selected in any of the three fields. The values in those fields are returned from the userform and captured in variables named: 'SelectedCatCode', 'SelectedVendorName' and 'SelectedYear'. After checking to see if no values have been selected in a given field, I construct the WHERE statement string in variable 'CriteriaString' and then form up the sql query.


    Here are samples of the contents of 'CriteriaString' when the code runs and when it doesn't.
    - When 'CriteriaString' contains either of the following, the code works:
    'CriteriaString' = USER_VI_BM_AVAIL_STOCK_STAT.CATEG_COD IN ('1') AND USER_VI_BM_AVAIL_STOCK_STAT.ATTRIBUTE_CODE_1 IN ('2014')
    'CriteriaString' = USER_VI_BM_AVAIL_STOCK_STAT.VEND_NAM IN ('ATOMIC SKI USA INC.') AND USER_VI_BM_AVAIL_STOCK_STAT.ATTRIBUTE_CODE_1 IN ('2014')
    - when 'CriteriaString' contains the following, I get the Type 13 error
    'CriteriaString' = USER_VI_BM_AVAIL_STOCK_STAT.CATEG_COD IN ('1') AND USER_VI_BM_AVAIL_STOCK_STAT.VEND_NAM IN ('ATOMIC SKI USA INC.') AND USER_VI_BM_AVAIL_STOCK_STAT.ATTRIBUTE_CODE_1 IN ('2014')


    You can see from above, any two of the three fields seem to work fine. But when the string length gets too long, above about 150, I get the error.


    Any clue what's causing the error to be thrown when it hits the sql query line of code or how I can better trouble shoot it? From reading, it sounds like these Error 13's can be a bit mysterious. Many thanks for the help in advance!





    Thanks for adding the code tags pike. Thought I did it correctly but obviously not.

  • Re: Fix "Run-Time error 13: Type MisMatch" in sql query with complex "Where" statemen


    Have you tried doing a debug.print str value and executing that directly on the server? It's normally a syntax error that throws error 13

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!