Skip to content
Advertisement

VBA ADODB error (80040e14) when trying to retrieve data from SQL Server

I am trying to connect to SQL Server from Excel VBA using the following code

and with some specific simple queries it actually works like a charm.

My problem shows up whenever I try to run the query right below to which I get the following error:

‘-2147217900 (80040e14)’ An expression of non-boolean type specified in a context where a condition is expected, near ‘ir’.

I’m no DBA or anything like that, the query above was being used on SQL Server to extract data and then copy it into Excel, so all I wanted to do was trying to fetch that with ADODB so I would kill the manual process.

I’ve been searching for related problems on the web and I actually came up understanding that the problem is in the query itself. Thing is, I can’t understand how is this possible since the above query actually works when executed manually on SQL Server.

I’d be really grateful if anybody could enlighten me with the solution to this problem.

Thanks.

Advertisement

Answer

Finally found the solution!

I followed Dale K‘s hint about stripping down the code to the minimum required and came up the the conclusion that it actually was a problem about maximum query length topping higher than 8000 characters (just like AlwaysLearning suggested).

The problem happened whenever i added .Text at the very end of the query variable, causing this 8k characters limit and an error output while executing the VBA.

I solved it by calling the query variable and adding .Value at the very end of it. (str_QuerySQL.Value) and now i managed to run even queries that have 15k+ characters.

Thank you all for the hints.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement