Results 1 to 9 of 9
  1. #1
    WickidWe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    123

    If Else in ms access query

    I have a query that is part reliant on a Selected item in a combobox
    I need to add to the query if [forms]![form1]![cboFunction] = 1 sort FGrp(Function group) equal to 1 in desending order


    else sort FGroup in assending order.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,622
    Never seen this so just tried a dynamic ORDER BY clause in a query object. Doesn't work.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    1) If your function group is a numeric field, then you can create a sortgroup field that is 1*functiongroup if you want it ascending, or is -1*sortgroup if you want it descending.

    2) If you build your SQL in VBA, then you can add the DESC if the control says to do so.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,622
    Okay, that works. Can't conditionally select the DESC or ASC parameter but can do the math. Example:

    SELECT FAAID FROM Airports ORDER BY ID * [enter value 1 for ascending or -1 for descending] ASC;

    This just clarified for me that SQL keywords cannot be conditional in Access query object.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    WickidWe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    123
    Sorry i Gave you slightly wrong information. Instead of the function group = 1
    the details are as follows.
    If the Function ID (from the combobox) = 1
    order by function group count where the group function =1 (which is in a query) Assending
    and where the Function ID is not 1 order by the same group count desending

    does this make any sense?

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,622
    Not really but doesn't matter if I understand. Apparently the only way to get this to work in a query object referencing combobox is if there is a numeric field that can be multiplied by 1/-1 to affect the sort order.

    ORDER BY [some field name] * IIf([Forms!formname!comboboxname]=1,1,-1) ASC
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    WickidWe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    123
    I tried to input into my query as you have above but was coming up with syntax error below is my original query without your code.

    SELECT qryAvailable.*, qryPickCount.pick AS Pick, qryFctnCount.Fctn AS Fctn, qryGrpCount.FGrp AS FGrp
    FROM ((qryAvailable LEFT JOIN qryPickCount ON qryAvailable.Name = qryPickCount.name) LEFT JOIN qryGrpCount ON qryAvailable.Name = qryGrpCount.Name) LEFT JOIN qryFctnCount ON qryAvailable.Name = qryFctnCount.name
    ORDER BY qryPickCount.pick, qryAvailable.Name;

    can you please let me know where to input your code.

    sorry if it seems silly but I am just a newbie to Access

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,622
    I was neglecting some [].

    This is the only thing that makes sense to me.

    ORDER BY qryPickCount.pick * IIf([Forms]![form1]![cboFunction]=1,1,-1), qryAvailable.Name
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    WickidWe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    123
    Great! that worked!

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

Similar Threads

  1. Replies: 3
    Last Post: 04-24-2012, 08:32 AM
  2. Replies: 34
    Last Post: 02-15-2012, 05:17 AM
  3. Replies: 12
    Last Post: 05-22-2011, 03:49 PM
  4. Convert Access query to SQL Server query
    By mkallover in forum Queries
    Replies: 2
    Last Post: 05-13-2011, 06:20 AM
  5. Replies: 10
    Last Post: 02-02-2011, 05:48 PM

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