Results 1 to 4 of 4

Thread: MS Acc: Referencing one column from another in a query

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2001
    Location
    Buffalo, NY
    Posts
    297

    MS Acc: Referencing one column from another in a query

    Hey All,

    I'm trying to reference a named column in the formula of another column within one query.

    i.e.

    SELECT IIF(X=Y,"1","2") AS Column1, IIF([Column1]="1","B","C") AS Column2
    FROM WHEREVER



    But it ONLY works if I don't try to filter Column2. If I add a criteria to it, the WHERE clause cannot refer to Column1. This following line, for example, would not work. It would cause Access to prompt me for Column1.

    WHERE IIF([Column1]="1","B","C") = "C"


    My actual code is much more complicated and I do not want to nest too many IIFs or repeat my code too often, any way to accomplish what I'm after?

    Thanks,

    Ben

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: MS Acc: Referencing one column from another in a query

    The only workaround I can think of is to put the calculated value into a table (possibly a temp table), and query using that (along with apt joins).

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2001
    Location
    Buffalo, NY
    Posts
    297

    Re: MS Acc: Referencing one column from another in a query

    Yea,

    I ended up, for the time being and probably permanently, making another query based off of this one that does the filtering.

    Ah well,

    Thanks anyways.

    -Ben

  4. #4
    Hyperactive Member
    Join Date
    May 2005
    Posts
    324

    Re: MS Acc: Referencing one column from another in a query

    It would seem that you could do the same thing using another table instead of the iifs.
    Supposing you had a table (tblLookup) with
    C1 C2 C3
    Y 1 B
    Z 2 C

    in it, then your query could become

    SELECT C1 AS Column1, C2 AS Column2
    FROM WHEREVER INNER JOIN tblLookup on Wherever.x = tblLookup.C1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width