Avatar of bejhan
bejhan

asked on 

SQL Syntax Error (Not a valid name)

I get an error with the following SQL statement: The SQL Statement is not a valid name. Make sure that it does not include invalid characters or punctuation that is too long.
SELECT DISTINCT CustomerID, CustomerLastActivity FROM ltblAllItems WHERE CollectorID = 'DEFAULT' AND (ISNULL(CollectionStatus) OR CollectionStatus = '');

Open in new window

SQL

Avatar of undefined
Last Comment
fhlio_admin
Avatar of brad2575
brad2575
Flag of United States of America image

Try this:

SELECT DISTINCT CustomerID, CustomerLastActivity FROM ltblAllItems WHERE CollectorID = 'DEFAULT' AND (CollectionStatus IS NULL) OR CollectionStatus = '');

Avatar of bejhan
bejhan

ASKER

Same problem.
Avatar of bejhan
bejhan

ASKER

Okay I put it in query creator in MS Access and it changed it to this which works...
SELECT DISTINCT ltblAllItems.CustomerID, ltblAllItems.CustomerLastActivity
FROM ltblAllItems
WHERE (((ltblAllItems.CollectorID)='DEFAULT') AND ((ltblAllItems.CollectionStatus) Is Null Or (ltblAllItems.CollectionStatus)=''));

Open in new window

Avatar of bejhan
bejhan

ASKER

Hmm but when I put that back in my code it gives the same problem...
Avatar of Guy Hengel [angelIII / a3]
use " instead of '
SELECT DISTINCT CustomerID, CustomerLastActivity FROM ltblAllItems WHERE CollectorID = "DEFAULT" AND (ISNULL(CollectionStatus) OR CollectionStatus = "");

Open in new window

Avatar of fhlio_admin
fhlio_admin
Flag of United States of America image

Is CollectorID numeric?
Are you using something other than MS Access?

If CollectorID is numeric then you would need to use a variable instead of a string.
If you are using something other than MS Access, it MIGHT not be able to handle single quotes, try double.
Avatar of bejhan
bejhan

ASKER

CollectorID is a string and I am using MS Access.
Avatar of bejhan
bejhan

ASKER

angelll, the double quotes gives me a syntax error in VBA.
Avatar of fhlio_admin
fhlio_admin
Flag of United States of America image

Try this?

SELECT DISTINCT CustomerID, CustomerLastActivity
FROM ltblAllItems
WHERE CollectorID = 'DEFAULT' AND (Nz((CollectionStatus), '')='')

Avatar of bejhan
bejhan

ASKER

Okay that works when I build a query of it, but when I try to create a querydef in VBA I get the same error, which is what happened to me before:

https://www.experts-exchange.com/questions/23586248/SQL-Syntax-Error-Not-a-valid-name.html?anchorAnswerId=22062843#a22062843


Set ARCustomersQR = CurrentDb.CreateQueryDef("SELECT DISTINCT CustomerID, CustomerLastActivity FROM ltblAllItems WHERE CollectorID = 'DEFAULT' AND (Nz((CollectionStatus), '')='')")

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of fhlio_admin
fhlio_admin
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of bejhan
bejhan

ASKER

Oh jeez I feel like an idiot, it has been a long day. Sorry for not including that earlier!
Avatar of bejhan
bejhan

ASKER

I should have known that... but I'm glad because I didn't know you could use NZ in there.
Avatar of fhlio_admin
fhlio_admin
Flag of United States of America image

Dude... it's cool... no worries...
SQL
SQL

SQL (Structured Query Language) is designed to be used in conjunction with relational database products as of a means of working with sets of data. SQL consists of data definition, data manipulation, and procedural elements. Its scope includes data insert, query, update and delete, schema creation and modification, and data access control.

61K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo