Syntax error in Access 2000 query

I have inherited a complex SQL query for Access 2000 which I have attached. It is not too important what the query does but I have had to modify it by adding the following statement in line 6 of the first SELECT clause:

(Select TOP 1 ItemCategories.Enable FROM ItemCategories WHERE Items.CategoryRefID = ItemCategories.RefId) AS CategoryEnabled

In the final WHERE clause I then use the result of this query by adding the following statement in line 34:

AND CategoryEnabled = True

Unfortunately when I run the query I get the following error:

Microsoft OLE DB Provider for ODBC Drivers error '80040e10'
[Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.

If I leave the additional WHERE clause statement out the query runs without error but I need it to check the CategoryEnabled flag.

Any help would be appreciated.


SELECT 
   ItemTexts.Name, 
   t2.rank, 
   t1.stockid, 
   items.*, 
   (Select TOP 1 ItemCategories.Enable FROM ItemCategories WHERE Items.CategoryRefID = ItemCategories.RefId) AS CategoryEnabled 
FROM 
   ( items inner join 
     ( SELECT 
              t1.rank, t1.StockID, pi1.ItemRefID 
       FROM 
              priceditems pi1 
       INNER JOIN 
              ( SELECT 
                        o1.stockid,count(o1.stockid) as rank 
                FROM 
                        orders AS o1 
                WHERE 
                        o1.transactionid IN 
                           ( SELECT DISTINCT 
                                  o2.TransactionID 
                             FROM 
                                  PricedItems pi2 
                                  LEFT JOIN orders o2 ON pi2.StockID = o2.StockID 
                             WHERE ((pi2.ItemRefID=46245974) and o2.transactionid<>'') ) 
                 GROUP BY o1.stockid ) as t1 
         ON pi1.stockid=t1.stockid 
         WHERE 
               pi1.StockStatusRefID <> -1 AND pi1.itemrefid <> 46245974 ) as t2 
       ON items.refID=t2.ItemRefID ) 
      LEFT JOIN itemTexts ON itemtexts.RefId=items.RefID 
WHERE 
     ItemTexts.lang='en' 
     AND CategoryEnabled = True 
     AND Items.Enable=True 
OREDR BY 
     t2.rank desc

Open in new window

mike99cAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mbizupCommented:
The "too few paramaters" error means that a field name is unrecognized.

If this runs fine without the WHERE clause as you stated, scrutinize the WHERE clause for typos or incorrect field names.
0
jparulCommented:

When you run the query without the WHERE clause, do you see CategoryEnabled as one of the output columns?
Is it possible for you to post a sampel of output you get without the WHERE clause?
0
mike99cAuthor Commented:
To answer both responses, I don't think it is a typo but for some reason it does not recognise the alias name of CategoryEnabled which was introduced at the end of line 6.

Removing the WHERE clause would certainly not produce CategoryEnabled as an output as the alias would be removed.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

GRayLCommented:
Why not put the addition WHERE clause in line 6?  BTW, with all the SELECTS flying around, it might be helpful to qualify the field CategoryEnabled with its table name which I presume is ItemCategories.
0
mike99cAuthor Commented:
Hi GRayL,
Can you please show me what you actually mean. I am not sure how to qualify the CategoryEnabled field with the ItemCategories table. When I tried I kept getting errors because of all the joins. Can you please help me here by showing how I can modify the SQL?
Thanks
0
GRayLCommented:
Line 6 becomes:

(Select TOP 1 ItemCategories.Enable FROM ItemCategories WHERE Items.CategoryRefID = ItemCategories.RefId AND ItemCatgegories.Enable = True) AS CategoryEnabled

Be aware you cannot use an alias which you used in the SELECT portion of a query after THE FROM clause - you have to repeat the whole subquery.  CategoryEnabled is an alias.  Is is not just as easy to restrict the selection in the subquery??
0
mike99cAuthor Commented:
Thanks for the suggestion GRayL but the solution you have given is not going to work as I need to read the CategoryEnabled alias so that I can restrict the rows returned from the query where CategoryEnabled=False.

I have taken a step back and now realise that I may have over complicated this. I have attached a modifed code which I believe is closer to what I require. The chages are as follows:
  1. Replaced nested SELECT query with
    ItemCategories.Enable
  2. Added extra inner join statement before last WHERE clause.
    INNER JOIN ItemCategories ON ItemCategories.RefID = items.RefID
  3. Replaced AND CategoryEnabled = True in last WHERE clause with
    AND ItemCategories.Enable = True
I now get the following error:
[Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression 'itemtexts.RefId=items.RefID inner join ItemCategories ON ItemCategories.RefID = items.RefID'.
I believe there is a syntax issue with the join that I added. I am more familiar with SQL server so I may have got the syntax a little wrong for Access SQL.

SELECT 
   ItemTexts.Name, 
   t2.rank, 
   t1.stockid, 
   items.*, 
   ItemCategories.Enable
FROM 
   ( items inner join 
     ( SELECT 
              t1.rank, t1.StockID, pi1.ItemRefID 
       FROM 
              priceditems pi1 
       INNER JOIN 
              ( SELECT 
                        o1.stockid,count(o1.stockid) as rank 
                FROM 
                        orders AS o1 
                WHERE 
                        o1.transactionid IN 
                           ( SELECT DISTINCT 
                                  o2.TransactionID 
                             FROM 
                                  PricedItems pi2 
                                  LEFT JOIN orders o2 ON pi2.StockID = o2.StockID 
                             WHERE ((pi2.ItemRefID=46245974) and o2.transactionid<>'') ) 
                 GROUP BY o1.stockid ) as t1 
         ON pi1.stockid=t1.stockid 
         WHERE 
               pi1.StockStatusRefID <> -1 AND pi1.itemrefid <> 46245974 ) as t2 
       ON items.refID=t2.ItemRefID ) 
      LEFT JOIN itemTexts ON itemtexts.RefId=items.RefID
      INNER JOIN ItemCategories ON ItemCategories.RefID = items.RefID
WHERE 
     ItemTexts.lang='en'
     AND ItemCategories.Enable = True  
     AND Items.Enable=True 
OREDR BY 
     t2.rank desc

Open in new window

0
jparulCommented:
You have an extra closing parenthesis before
LEFT JOIN itemTexts ON itemtexts.RefId=items.RefID
Try this
 

SELECT 
   ItemTexts.Name, 
   t2.rank, 
   t1.stockid, 
   items.*, 
   ItemCategories.Enable 
   FROM items 
      inner join 
		     ( SELECT 
		              t1.rank, t1.StockID, pi1.ItemRefID 
			FROM 
		              priceditems pi1 
		       INNER JOIN 
		              ( SELECT o1.stockid,count(o1.stockid) as rank 
		                FROM 
		                        orders AS o1 
                		WHERE 
		                        o1.transactionid IN 
                				( SELECT DISTINCT o2.TransactionID 
			                          FROM PricedItems pi2 
		                                  LEFT JOIN orders o2 ON pi2.StockID = o2.StockID 
                			          WHERE ((pi2.ItemRefID=46245974) and o2.transactionid<>'') 
						) 
		                 GROUP BY o1.stockid ) 
				as t1 
			        ON pi1.stockid=t1.stockid 
			         WHERE pi1.StockStatusRefID <> -1 AND pi1.itemrefid <> 46245974 
			) as t2 
       ON items.refID=t2.ItemRefID  
      LEFT JOIN itemTexts ON itemtexts.RefId=items.RefID
      INNER JOIN ItemCategories ON ItemCategories.RefID = items.RefID
WHERE 
     ItemTexts.lang='en'
     AND ItemCategories.Enable = True  
     AND Items.Enable=True 
OREDR BY 
     t2.rank desc

Open in new window

0
mike99cAuthor Commented:
Hi iparul,
Thanks for the new query but when I tried it I got the following error:

Microsoft OLE DB Provider for ODBC Drivers error '80040e14'
[Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression 'items.refID=t2.ItemRefID left join itemTexts on itemtexts.RefId=items.RefID inner join ItemCategories ON ItemCategories.RefID = items.RefID'.


Remember that the query is for Access 2000. I think it may need extra brackets but I am not sure where.
0
mike99cAuthor Commented:
Can anyone please assist me with the attached query?

I am not familiar with the Access 200 SQL JOIN syntax. Can someone please assist me with the join syntax?
SELECT 
   ItemTexts.Name, 
   t2.rank, 
   t1.stockid, 
   items.*, 
   ItemCategories.Enable 
   FROM items 
      inner join 
		     ( SELECT 
		              t1.rank, t1.StockID, pi1.ItemRefID 
			FROM 
		              priceditems pi1 
		       INNER JOIN 
		              ( SELECT o1.stockid,count(o1.stockid) as rank 
		                FROM 
		                        orders AS o1 
                		WHERE 
		                        o1.transactionid IN 
                				( SELECT DISTINCT o2.TransactionID 
			                          FROM PricedItems pi2 
		                                  LEFT JOIN orders o2 ON pi2.StockID = o2.StockID 
                			          WHERE ((pi2.ItemRefID=46245974) and o2.transactionid<>'') 
						) 
		                 GROUP BY o1.stockid ) 
				as t1 
			        ON pi1.stockid=t1.stockid 
			         WHERE pi1.StockStatusRefID <> -1 AND pi1.itemrefid <> 46245974 
			) as t2 
       ON items.refID=t2.ItemRefID  
      LEFT JOIN itemTexts ON itemtexts.RefId=items.RefID
      INNER JOIN ItemCategories ON ItemCategories.RefID = items.RefID
WHERE 
     ItemTexts.lang='en'
     AND ItemCategories.Enable = True  
     AND Items.Enable=True 
OREDR BY 
     t2.rank desc

Open in new window

0
jparulCommented:
Hi
Sorry for getting back to you late. Anyways, I don't have Access 2000, so I am unable to test the query. But I tried to format the query with some parenthesis and put a ; at th end. Try and see if this works:
 

SELECT 
   ItemTexts.Name, 
   t2.rank, 
   t1.stockid, 
   items.*, 
   ItemCategories.Enable 
   FROM (((items 
      inner join 
		     ( SELECT 
		              t1.rank, t1.StockID, pi1.ItemRefID 
			FROM 
		              priceditems pi1 
		       INNER JOIN 
		              ( SELECT o1.stockid,count(o1.stockid) as rank 
		                FROM 
		                        orders AS o1 
                		WHERE 
		                        o1.transactionid IN 
                				( SELECT DISTINCT o2.TransactionID 
			                          FROM PricedItems pi2 
		                                  LEFT JOIN orders o2 ON pi2.StockID = o2.StockID 
                			          WHERE ((pi2.ItemRefID=46245974) and o2.transactionid<>'') 
						) 
		                 GROUP BY o1.stockid ) 
				as t1 
			        ON pi1.stockid=t1.stockid 
			         WHERE pi1.StockStatusRefID <> -1 AND pi1.itemrefid <> 46245974 
			) as t2 
       ON items.refID=t2.ItemRefID) 
      LEFT JOIN itemTexts ON itemtexts.RefId=items.RefID)
      INNER JOIN ItemCategories ON ItemCategories.RefID = items.RefID)
WHERE 
     ItemTexts.lang='en'
     AND ItemCategories.Enable = True  
     AND Items.Enable=True 
ORDER BY 
     t2.rank desc ;

Open in new window

0
mike99cAuthor Commented:
Hi,
Thanks for getting back to me. We are getting a lot closer. I executed the query and there were no errors. However the query did not return any results which is unexpected as it should return several rows.
Do you think the last inner join is in the right place?
0
jparulCommented:
I think the joins are in right place. If the query is not returning any results, then there is a possibility that all the conditions are not being met. Did you check that all the join conditions and where conditions are okay and that there are rows that satisfy those conditions?
0
mike99cAuthor Commented:
Hi iparul,

If it helps, I posted another post here:
http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_24438548.html

In it I show the original query which does work and what I actually want to add. You may want to refer to this post as it gives a better starting point.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jparulCommented:
Okay
Here's what I would do:
Starting from the query that worked, and looking at the final query we have which doesn't give any errors, try adding one clause (where condition, join, additional col in select etc) one at a time and see if it gives the expected results. Here's the order I would do it
a) Add ItemCategories.Enable  in the select statement
b) Add inner join statement
c) Add condition for ItemCategories.Enable = True  
See if this helps and let me know.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.