Office

  • Office 2003
  • Office
  • All forums
Question

Sub-Query headaches

I keep having problem with sub-queries and I hope someone can tell me why and what to do to resolve this ongoing problem once and for all!

The sub-query itself does not matter.  When I create it, it will have the general format

SELECT Tmp1.[Field1Name], ...
FROM (

SELECT ...

) AS Tmp1

 

If I save it and run it all is good.

 

Now, for unknow reasons (perhaps compact and repair, I don't know), I get error messages and when I open the query in design mode my query has been transformed from () brackets to [] and an extra . is added making the query not work, if I edit it back to its original form, it works again....   But this keeps happening in multiple databases!!!

SELECT Tmp1.[Field1Name], ...
FROM [

SELECT ...

]. AS Tmp1

 

Please help, someone!  Am I writing my queries wrong in the first place?  What am I doing wrong?

I am running WinXP and Office 2003 SP3  (everything is supposedly up-to-date).

Thank you!

QuestionBoy

    • Child exploitation or abuse
    • Harassment or threats
    • Inappropriate/Adult content
    • Nudity
    • Profanity
    • Software piracy
    • SPAM/Advertising
    • Virus/Spyware/Malware danger
    • Other Term of Use or Code of Conduct violation
1 Person had
this question

Was this helpful?

1

Vote

Answer

I keep having problem with sub-queries and I hope someone can tell me why and what to do to resolve this ongoing problem once and for all!

The sub-query itself does not matter.  When I create it, it will have the general format

SELECT Tmp1.[Field1Name], ...
FROM (

SELECT ...

) AS Tmp1

 

If I save it and run it all is good.

 

Now, for unknow reasons (perhaps compact and repair, I don't know), I get error messages and when I open the query in design mode my query has been transformed from () brackets to [] and an extra . is added making the query not work, if I edit it back to its original form, it works again....   But this keeps happening in multiple databases!!!

SELECT Tmp1.[Field1Name], ...
FROM [

SELECT ...

]. AS Tmp1

 

Please help, someone!  Am I writing my queries wrong in the first place?  What am I doing wrong?

I am running WinXP and Office 2003 SP3  (everything is supposedly up-to-date).

Thank you!

QuestionBoy


This is a well-known and annoying problem.  Access has a preferred, non-standard SQL syntax for derived tables, and if you give it half a chance, it will transform your SQL into that syntax.  The standard syntax is:

    (SELECT ... FROM ...) As T

Jet's preferred, but non-standard syntax is:

    [SELECT ... FROM ...;]. As T

.. or some variation thereof.  As you see, the parentheses have been changed to square brackets, and a dot (.) has been added after the closing bracket.

Now, this doesn't matter if the subquery doesn't include any square brackets of its own -- around field or table names, for example.  But sometime those are required because of names that use nonstandard characters, and also the Access query designer automatically surrounds everything with brackets "for safety's sake."  So in these cases, the transformed query becomes syntactically invalid, because Access can't parse brackets inside of brackets.  Once this happens, if the SQL has to be reparsed, an error is raised.

The first workaround for this problem is to ensure that no brackets are present around field names or table names in the SQL of the subquery.  If you can do that, then there will be no problem if Access decides to rewrite your SQL in its own quirky way.

If you can't do that, then you need to design the query in SQL view, and try never to open the query in design view.  If you do open it in design view, you must not then save the query, unless you first switch to design view and fix the SQL again.

This is particularly a problem in rowsources for combo and list boxes, since clicking the build button on the RowSource property line automatically opens the query in design view.  Flip to SQL view, fix it if necessary, and do your work there.

We have asked Microsoft to fix this bug in the query designer -- so far, to no effect.

 

    • Child exploitation or abuse
    • Harassment or threats
    • Inappropriate/Adult content
    • Nudity
    • Profanity
    • Software piracy
    • SPAM/Advertising
    • Virus/Spyware/Malware danger
    • Other Term of Use or Code of Conduct violation

Dirk Goldgar

Dirk Goldgar, MS Access MVP
Access tips: www.datagnostics.com/tips.html

Was this helpful?

Now that's what I call an answer!

Thank you so very much for the detailed explanation!!!

In this instance I am dealing with a horribly designed db where I need the [] brackets everywhere, so I will continue to fix these errors when they arise.

In future work, where naming conventions are properly followed, I will make sure not to use [] brackets within these queries to avoid the problem altogether.

Thank you once again for sharing your wealth of knowledge with the rest of us!  Hopefully, Microsoft will fix this problem (perhaps one day)! 

    • Child exploitation or abuse
    • Harassment or threats
    • Inappropriate/Adult content
    • Nudity
    • Profanity
    • Software piracy
    • SPAM/Advertising
    • Virus/Spyware/Malware danger
    • Other Term of Use or Code of Conduct violation

Message marked as answers cannot be deleted

To delete this message, first unmark this message as an answer, then delete it.

Reason to remove escalation


Merge

Enter the thread ID of the thread you are merging into


Reply will be posted to a public thread

You are replying to a public portion of this thread. To reply privately, click Cancel, click the Private Messages tab, and Reply on that private message.

Don't show this message again

To report abuse, sign in or continue without signing in

Thank you.

Report abuse

Abuse type:

Details (optional):

Report abuse

Abuse type:

Details (required):
Enter the characters you see (required):
Type the numbers that you see in the picture.
Play audio and type the numbers that you hear.
Show a different picture.

Sign in

Hotmail, Xbox Live, Messenger, or msn accounts will also work.

Don't have one of the above accounts?

Signing in...
This page will automatically update after you are signed in.
If you are having problems, you can close this message and try to connect again.