How do I use the WITH statement in Access?

12,725

As others have stated in the comments, Access SQL does not support the with keyword the same way that TSQL does. You can accomplish close to the same thing though by writing the first query and saving it. The saved query can then be referenced in your Access SQL as though it was a table (similar to creating a view in TSQL).

Others also noted that VBA can use the with keyword, but for a different purpose.

Share:
12,725
quant
Author by

quant

I'm here to learn programming and *nix mostly, and am immensely indebted to this community for always being there when I get stuck. The help and support I've received on SE sites has allowed me to develop my understanding in a range of disciplines much faster than I could ever have hoped for. I try to give back once in a while as well; it's the least I could do. I'm the owner and maintainer of www.portnovel.com

Updated on July 25, 2022

Comments

  • quant
    quant almost 2 years

    I'm using Access and would like to simplify some of my queries by naming SELECT statements. However, I can't use the WITH statement for some reason. I have tried running the following simple example to illustrate the problem:

    WITH T1 AS
    (
    SELECT * FROM [Transactions]
    )
    SELECT * FROM T1;
    

    Where Transactions is a table of dates and amounts. When I run this query I get the following error:

    Invalid SQL statement; expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'

    What am I doing wrong?