VBA & SQL Error

psycoperl

Active Member
Joined
Oct 23, 2007
Messages
339
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
  3. Web
I am getting a Run-time error '-2147217900 (80040e14)':
Syntax error (missing operator) in query expression
'(((Len(Trim(tblTempSimNetResultData.STUDENTID)))<>9)) or (((tblTempSimNetResultData.STUDENTID) Like "*" && " " && "*"))'.

Here is my SQL Code that I am trying to run. Any suggestions?

Code:
strCheckStudentIDSQL = "SELECT tblTempSimNetResultData.STUDENTID, 
tblTempSimNetResultData.SCORE, tblTempSimNetResultData.[EXAM DATE], 
tblTempSimNetResultData.[LAST NAME], tblTempSimNetResultData.[FIRST NAME] 
FROM tblTempSimNetResultData 
WHERE (((Len(Trim(tblTempSimNetResultData.STUDENTID)))<>9)) 
Or (((tblTempSimNetResultData.STUDENTID) Like ""*"" && "" "" && ""*""));"

It works in the Query SQL Editor but not when running it as part of my VBA.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
I had this same problem the other day. Try renaming your table to something shorter and see if it works. It worked on mine, although not sure why. I wouldn't have thought that I was reaching the limit of the string variable. Give it a try and let us know if it works. Then maybe someone can explain why?
 
Upvote 0
Try renaming your table to something shorter and see if it works.
No need to rename the table, just use a table alias in your query. i.e.

Code:
SELECT t.Field1, t.Field2, t.Field3
FROM Table1 as t
WHERE Len(t.Field)<>9;
 
Upvote 0
No need to rename the table, just use a table alias in your query. i.e.

Code:
SELECT t.Field1, t.Field2, t.Field3
FROM Table1 as t
WHERE Len(t.Field)<>9;

Thanks for that Joe4. But is there a limit to string length that you can use before you hit this problem?
 
Upvote 0
I am not sure. I imagine it may depend on how you have declared the variable you are storing the SQL string in. If it is declared as a String, it may be limited (to 255, maybe)? Off the top of my head, not sure, but it could be confirmed via some testing.
 
Upvote 0
My first thought was to change this
Code:
"Or (((tblTempSimNetResultData.STUDENTID) Like ""*"" && "" "" && ""*""));"
to this
Code:
"Or (((tblTempSimNetResultData.STUDENTID) Like '* *'));"

I've had issues with the double quotes resolving properly and have long since went to single quotes, especially where the statement is even somewhat complex.

hth,

Rich
 
Upvote 0
That's a good point, Rich.

When it starts getting to two to three sets of quotes, I often get confused. So I often use CHR(34) in my VBA code when I want to return "literal" quote marks.
 
Upvote 0
If you're using ADO in your VBA code try using % instead of * as the wildcard operator - if it's still not working.
 
Upvote 0
If you're using ADO in your VBA code try using % instead of * as the wildcard operator - if it's still not working.

At the risk of sounding like an absolute noob that I am, what's the difference between % and * ?
 
Upvote 0
At the risk of sounding like an absolute noob that I am, what's the difference between % and * ?

% is the SQL Server wildcard character and, I think, standard for most databases. * is Access-specific and rather unusual. I think that ADO can trip up on this - can't remember for sure...

Access:
Like '*fish*'

SQL Server:
Like '%fish%'

Note that we can use single quotes in the SQL statements too and that spares some of the double quote problems that arise when writing VBA string literals (requiring double quotes)
 
Upvote 0

Forum statistics

Threads
1,224,570
Messages
6,179,610
Members
452,931
Latest member
The Monk

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top