Ask the Expert

Syntax differences in MS Access

The query you gave in the answer Consolidate data on multiple rows into one (01 August 2003) is not working in MS Access 2002. Can you please tell me why?

    Requires Free Membership to View

There are three differences in syntax that need to be made so that you can run the query in MS Access.

First, the CASE constructions must be rewritten with IIF functions:

select Name
     , IIF(Food='Fruit','Fruit','') as Fruit
     , IIF(Food='Banana','Banana','') as Banana
     , IIF(Food='Bread','Bread','') as Bread
     , IIF(Food='Pizza','Pizza','') as Pizza
  from yourtable

Next, MS Access does not support derived tables. Well, it didn't in earlier versions, and I have not tested Access 2002, but in any case, there's a workaround. Just save the above query as query ZZZ (or some other name), and then you can use it in the FROM clause.

Finally, concatenation in Access is different, and uses the ampersand. (Access 97 gives the error message "Invalid use of vertical bars" which always struck me as hilarious. I mean, if it's smart enough to recognize them as vertical bars, why doesn't it just do the concatenation? The double vertical bars or pipes are the standard SQL concatenation operator.)

So the query becomes:

select Name
     , max(Fruit)
       & ' ' & max(Banana)
       & ' ' & max(Bread)
       & ' ' & max(Pizza)  as Food
  from ZZZ
group by Name

This was first published in September 2003

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: