Results 1 to 10 of 10
  1. #1
    Remster is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    317

    ORDER BY ... If (SQL syntax question)

    Hi Folks



    I have the following as the Row Source for a list box:

    SELECT Field1, Field2 ORDER BY Field1, Field2;

    Supposing the possible values for Field1 are 1 and 2, how do I get Field2 to sort in ascending order if the value in Field1 is 1 but in descending order if the value in Field1 is 2? I'm guessing it's something like this, but I don't know enough about SQL to work out the correct syntax:

    SELECT Field1, Field2 ORDER BY Field1, If(Field1=1, Field2 ASC, Field2 DESC);

    Any help would be very welcome.

    Thanks

    Remster

  2. #2
    khalid's Avatar
    khalid is offline MS-Access Developer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2010
    Location
    Kuwait
    Posts
    244
    I don't think it would work but try this:

    SELECT Field1, Field2 ORDER BY Field1, IIf(Field1=1, Field2 ASC, Field2 DESC);
    IF is not the correct syntax in you select statement.

  3. #3
    Remster is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    317
    Alas, I get an error message (the same error message) telling me there's a missing operator in the expression.

  4. #4
    Rixxe is offline Blessed Bear
    Windows 7 Access 2007
    Join Date
    Sep 2010
    Location
    London (Essex ... yep!)
    Posts
    184
    Missing a FROM?

    SELECT Field1, Field2 ORDER BY Field1, IIf(Field1=1, Field2 ASC, Field2 DESC) FROM Table1;

  5. #5
    Remster is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    317
    No, I have the 'FROM' there. I just left it out of my example for simplicity.

  6. #6
    Remster is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    317
    I've tried a few more things and the problem seems to lie in including 'ASC' and 'DESC' in the 'IIf' statement. The following, for example, works fine:

    SELECT Field1, Field2, Field3 ORDER BY Field1, IIf(Field1=1, Field2, Field3);

    So how do I smuggle in 'ASC' and 'DESC' without generating a syntax error?


  7. #7
    khalid's Avatar
    khalid is offline MS-Access Developer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2010
    Location
    Kuwait
    Posts
    244
    Hi,
    You are right you can not use ASC, DEC in the form of IIf(Field1=1, Field2 ASC, Field2 DESC) which would definitely produce a syntax error.
    However what about this:
    SELECT Field1, Field2 ORDER BY Field1, IIf(Field1=<Field2, Field1, Field2);

  8. #8
    Remster is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    317
    Hmm, that doesn't work, but I'm not sure why it would. What I'm trying to achieve is this:
    • First all records are sorted by Field1.
    • Then all records where Field1=1 are sorted by Field2 in descending order.
    • And all records where Not(Field1=1) are sorted by Field2 in ascending order.
    For example:

    Field1_________Field2
    1______________Z
    1______________Y
    1______________X
    1______________W
    1______________V
    2______________V
    2______________W
    2______________X
    2______________Y
    2______________Z
    3______________V
    3______________W
    3______________X
    3______________Y
    3______________Z

  9. #9
    Remster is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    317
    Got it at last, mostly through trial and error:

    SELECT Field1, Field2 ORDER BY Field1, IIf(Field1=1, TableName.Field2, "") DESC, IIf(Not(Field1=1), TableName.Field2, "") ASC;

  10. #10
    khalid's Avatar
    khalid is offline MS-Access Developer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2010
    Location
    Kuwait
    Posts
    244
    Great! that you got through

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Id Order
    By Rockin-John in forum Forms
    Replies: 15
    Last Post: 05-03-2011, 02:39 PM
  2. simple syntax question
    By markjkubicki in forum Programming
    Replies: 6
    Last Post: 09-14-2010, 04:13 PM
  3. Incomplete Syntax Clause (syntax error)
    By ajetrumpet in forum Programming
    Replies: 4
    Last Post: 09-11-2010, 10:47 AM
  4. Order by
    By cowboy in forum Access
    Replies: 2
    Last Post: 05-03-2010, 05:04 PM
  5. Chart Order
    By protean_being in forum Reports
    Replies: 0
    Last Post: 06-19-2008, 09:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums