tags:

views:

62

answers:

3

I have a query in MS Access (2003) that makes use of a subquery. The subquery part looks like this:

...FROM (SELECT id, dt, details FROM all_recs WHERE def_cd="ABC-00123") AS q1,...

And when I switch to Table View to verify the results, all is OK.

Then, I wanted the result of this query to be printed on the page header for a report (the query returns a single row that is page-header stuff). I get an error because the query is suddenly re-written as:

...FROM [SELECT id, dt, details FROM all_recs WHERE def_cd="ABC-00123"; ] AS q1,...

So it's Ok that the round brackets are automatically replaced by square brackets, Access feels it needs to do that, fine! But why is it adding the ; into the subquery, which causes it to fail?

I suppose I could just create new query objects for these subqueries, but it seems a little silly that I should have to do that.

+1  A: 

Ah, the joys of Access. The query designer in general does not play well with derived tables. There are more than a few constructs in fact, that Jet will honor that cannot be viewed properly in the query designer. In fact, the QBE will mangle (alter as you have seen) many of these complex queries. In general, you should simply assume that you cannot safely view the design of a derived table or "complex" query in the QBE but instead only in code.

Thomas
Heh I didn't even use the query designer, just the SQL editor and then I tried to create a subreport on this query on the report's page header. So it looks like I need to create subquery objects after all, eh?
FrustratedWithFormsDesigner
@FrustratedWithFormsDesigner - The SQL editor on which you speak is a query designer. You can still create queries with derived tables in code and execute them just fine. You just cannot use the GUI tools to view the query or change its design. If you need a derived table and you want to view it in the GUI tool, then yes, you need to create a stored query and use that.
Thomas
@Thomas: And that's what I've done, create stored queries for the subqueries. I don't like it, but it works. *sigh*
FrustratedWithFormsDesigner
I use derived tables written dynamically in VBA all the time (usually because I need criteria on the derived table that are chosen at runtime). Not sure what forces you to use a saved QueryDef.
David-W-Fenton
+1  A: 

I have seen what you described where Access replaces subquery parentheses with square brackets. However I have never noticed it adding in a semicolon after the subquery.

Another detail is that, with square backets, your query will follow this pattern:

... FROM [ SELECT whatever FROM someTable ]. AS q ...

Notice the dot immediately after the closing square bracket. Your sample didn't include a dot. So I wonder what might happen if you add the dot and remove the semicolon (in SQL View) like this:

...FROM [SELECT id, dt, details FROM all_recs WHERE def_cd="ABC-00123" ]. AS q1,...

Does Access accept that change, and is that preserved when you make any further changes through the Query Designer?

HansUp
Yeah, I now notice it does that *until* I try to use the query as a subreport, at which point it mangles the subqueries. :(
FrustratedWithFormsDesigner
+2  A: 

If you want to use the more standard derived-table syntax, you need to switch to SQL 92 mode. However, beware that this also changes your wildcards to SQL-Server be compatible (% and _ instead of * and ?).

As @HansUp points out, the error in your SQL is not the ";" but the lack of the trailing period after the closing square bracket. That syntax has been part of Jet for as long as I've been using derived tables (which would be back to A97 or so). It has the flaw of preventing any expressions inside the derived-table SQL that require square brackets (such as field names with spaces in them), but I don't think that's a terrible flaw as I avoid naming things in ways that require square brackets.

David-W-Fenton
I didn't know I could switch to SQL 92. How do I do that?
FrustratedWithFormsDesigner
@Frustrated From the Access main menu, Tools -> Options. Then, on the Tables/Queries tab of the Options dialog, look in the lower right corner for SQL Server Compatible Syntax (ANSI 92). Select one of the two checkbox options.
HansUp
@HansUp: Thanks!! :)
FrustratedWithFormsDesigner
Also, keep in mind that with ADO, you use SQL 92, and with DAO, SQL 89, and have no choice one way or the other.
David-W-Fenton