Results 1 to 6 of 6

Thread: [RESOLVED] Where in sql only if not equal to <all>

  1. #1
    Fanatic Member
    Join Date
    Jul 06
    Location
    Anchorage, Alaska
    Posts
    545

    Resolved [RESOLVED] Where in sql only if not equal to <all>

    How would I perform a Where, in a SQL command, only if the item is not equal to "<All>"

    Currently I have

    WHERE Q4.AgentGroup= [Forms]![frmCDErrorReports]![Group]

    But, the first item in the dropdown [group] is "<All>"

    Is there a way to structure the Where so it doesn't limit anything if "<All>" is selected?

    Edit 1:

    I tried
    WHERE Q4.AgentGroup= iif([Forms]![frmCDErrorReports]![Group]='<All>','*',[Forms]![frmCDErrorReports]![Group])
    hoping that the * would allow the where to grab everything, but its just comparing the two, and because no group is = * it isn't giving any results.

    Edit 2:

    Just tried this also

    Code:
    WHERE iif(Forms!frmCDErrorReports!Group='<All>','','Q4.AgentGroup=Forms!frmCDErrorReports!Group')
    Didn't work, always gets all the agent groups.
    Last edited by rack; Feb 15th, 2008 at 03:26 PM.
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  2. #2
    Addicted Member
    Join Date
    Feb 08
    Location
    Hamburg
    Posts
    138

    Re: Where in sql only if not equal to <all>

    use "LIKE" instead of =

    then the asterisk (*) works fine


    Code:
    WHERE Q4.AgentGroup LIKE iif([Forms]![frmCDErrorReports]![Group]='<All>','*',[Forms]![frmCDErrorReports]![Group])

  3. #3
    Head Hunted anhn's Avatar
    Join Date
    Aug 07
    Location
    Australia
    Posts
    3,669

    Re: Where in sql only if not equal to <all>

    The "Like" may work but it is very slow if the table is large.

    There is a trick. If the filter equal "<All>" then pick any AgentGroup that "equal to itself", ie. Q4.AgentGroup = Q4.AgentGroup.
    That means pick all of them (except Null, because we can not compare Null=Null).

    WHERE Q4.AgentGroup = IIf([Forms]![frmCDErrorReports]![Group]="<All>", Q4.AgentGroup, [Forms]![frmCDErrorReports]![Group])

    For safety, you should also trap Null value as well, otherwise you will get error when the value of combobox [Group] is null (blank).

    WHERE Q4.AgentGroup = IIf(([Forms]![frmCDErrorReports]![Group]="<All>") Or ([Forms]![frmCDErrorReports]![Group] Is Null),
    Q4.AgentGroup, [Forms]![frmCDErrorReports]![Group])
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 02
    Location
    Bristol, UK
    Posts
    35,564

    Re: Where in sql only if not equal to <all>

    Another trick is to skip all of the field based comparisons if apt, eg:
    Code:
    WHERE (
              ([Forms]![frmCDErrorReports]![Group]="<All>") 
           OR (Q4.AgentGroup = [Forms]![frmCDErrorReports]![Group]) 
          )
    
    [AND ...]
    The first half (the comparison to "<All>") will only run once for the entire query, and if it is "<All>" then the second half (the line starting with "or") will not run at all.

    If the value is not "<All>", the standard comparison (the second half) will be made as usual for each row of data.


    At worst, this only adds one comparison to the overall query (which for 100+ rows of data is negligible) to using separate 'optimised' queries for each variation.

    anhn makes a good point about checking for Null in the control, and this can be added to the first half of this method.

  5. #5
    Head Hunted anhn's Avatar
    Join Date
    Aug 07
    Location
    Australia
    Posts
    3,669

    Re: Where in sql only if not equal to <all>

    si has shown a good way to write Criteria in this case, that is what I usually do in Access Query design.

    However, in Access and VBA, both parts A and B in the expression "A Or B" will be evaluated at any time, ie. B will be evaluated even when A is True.
    eg, both of these statements cause Overflow Run-time error:
    a = True Or CInt(10 ^ 5)
    b = IIf(True, 1, CInt(10 ^ 5))

    To save you to repeat a lengthy typing, in Query Design View you can enter:



    With that in SQL View you will see for the left:
    Code:
    WHERE ((([Forms]![frmCDErrorReports]![Group]) Is Null 
    Or ([Forms]![frmCDErrorReports]![Group])="<All>" 
    Or ([Forms]![frmCDErrorReports]![Group])=[Q4].[AgentGroup]));
    or for the right:
    Code:
    WHERE ((([Forms]![frmCDErrorReports]![Group]) Is Null)) 
    OR ((([Forms]![frmCDErrorReports]![Group])="<All>")) 
    OR ((([Forms]![frmCDErrorReports]![Group])=[Q4].[AgentGroup]));
    Don't worry about too many extra parentheses, that is the way Access put them.
    If writing SQL directly we can remove them:
    Code:
    WHERE ([Forms]![frmCDErrorReports]![Group] Is Null)
    OR ([Forms]![frmCDErrorReports]![Group]="<All>") 
    OR ([Forms]![frmCDErrorReports]![Group]=[Q4].[AgentGroup]);
    That is clear enough, but this is also OK:
    Code:
    WHERE [Forms]![frmCDErrorReports]![Group] Is Null
    OR [Forms]![frmCDErrorReports]![Group]="<All>" 
    OR [Forms]![frmCDErrorReports]![Group]=[Q4].[AgentGroup];
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  6. #6
    Fanatic Member
    Join Date
    Jul 06
    Location
    Anchorage, Alaska
    Posts
    545

    Re: Where in sql only if not equal to <all>

    Anhn As always thank you.

    Si_The_Geek thank you as well, you have been very helpfull.

    I'm going to spend the week changing the database to use what you guys have suggested, instead of me building a filter for the report, as this will both save space, and ensure that the data is filtered before going to the report.
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

Posting Permissions

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