Creating a query in MS Access that returns all entries if no input is supplied or the selected entries if...












1















I'm trying to create a query that will accept input from a form (from a set of 3 radio buttons, one for "male" one for "female" and one for "either") and return all the entries of the specified input.



I'm a bit of a newbie to MS Access so apologies if this is a super basic question. I've looked a lot online to find a solution but can't find one that works!



The query I have so far is this:



   IIf([Forms]![AgeRangeQueryForm]![GenderOptionGroup]. 
[Value]=1,"M",IIf([Forms]![AgeRangeQueryForm]![GenderOptionGroup].
[Value]=2,"F",IIf([Forms]![AgeRangeQueryForm]![GenderOptionGroup].
[Value]=3,([Child Details].[Gender])="M" Or ([Child Details].
[Gender])="F")))


If it helps the SQL code for the query is this:



   WHERE ((([Child Details].Gender)=IIf([Forms]![AgeRangeQueryForm]! 
[GenderOptionGroup].[Value]=1,"M",IIf([Forms]![AgeRangeQueryForm]!
[GenderOptionGroup].[Value]=2,"F",IIf([Forms]![AgeRangeQueryForm]!
[GenderOptionGroup].[Value]=3,([Child Details].[Gender])="M" Or ([Child
Details].[Gender])="F"))))


I can make this work for when Male and Female are selected, but unfortunately I can't get it to work if I select either. In this case it should be just returning all entries regardless of what gender is in the field but it just returns nothing. Any pointers would be appreciated!



Thanks!










share|improve this question



























    1















    I'm trying to create a query that will accept input from a form (from a set of 3 radio buttons, one for "male" one for "female" and one for "either") and return all the entries of the specified input.



    I'm a bit of a newbie to MS Access so apologies if this is a super basic question. I've looked a lot online to find a solution but can't find one that works!



    The query I have so far is this:



       IIf([Forms]![AgeRangeQueryForm]![GenderOptionGroup]. 
    [Value]=1,"M",IIf([Forms]![AgeRangeQueryForm]![GenderOptionGroup].
    [Value]=2,"F",IIf([Forms]![AgeRangeQueryForm]![GenderOptionGroup].
    [Value]=3,([Child Details].[Gender])="M" Or ([Child Details].
    [Gender])="F")))


    If it helps the SQL code for the query is this:



       WHERE ((([Child Details].Gender)=IIf([Forms]![AgeRangeQueryForm]! 
    [GenderOptionGroup].[Value]=1,"M",IIf([Forms]![AgeRangeQueryForm]!
    [GenderOptionGroup].[Value]=2,"F",IIf([Forms]![AgeRangeQueryForm]!
    [GenderOptionGroup].[Value]=3,([Child Details].[Gender])="M" Or ([Child
    Details].[Gender])="F"))))


    I can make this work for when Male and Female are selected, but unfortunately I can't get it to work if I select either. In this case it should be just returning all entries regardless of what gender is in the field but it just returns nothing. Any pointers would be appreciated!



    Thanks!










    share|improve this question

























      1












      1








      1








      I'm trying to create a query that will accept input from a form (from a set of 3 radio buttons, one for "male" one for "female" and one for "either") and return all the entries of the specified input.



      I'm a bit of a newbie to MS Access so apologies if this is a super basic question. I've looked a lot online to find a solution but can't find one that works!



      The query I have so far is this:



         IIf([Forms]![AgeRangeQueryForm]![GenderOptionGroup]. 
      [Value]=1,"M",IIf([Forms]![AgeRangeQueryForm]![GenderOptionGroup].
      [Value]=2,"F",IIf([Forms]![AgeRangeQueryForm]![GenderOptionGroup].
      [Value]=3,([Child Details].[Gender])="M" Or ([Child Details].
      [Gender])="F")))


      If it helps the SQL code for the query is this:



         WHERE ((([Child Details].Gender)=IIf([Forms]![AgeRangeQueryForm]! 
      [GenderOptionGroup].[Value]=1,"M",IIf([Forms]![AgeRangeQueryForm]!
      [GenderOptionGroup].[Value]=2,"F",IIf([Forms]![AgeRangeQueryForm]!
      [GenderOptionGroup].[Value]=3,([Child Details].[Gender])="M" Or ([Child
      Details].[Gender])="F"))))


      I can make this work for when Male and Female are selected, but unfortunately I can't get it to work if I select either. In this case it should be just returning all entries regardless of what gender is in the field but it just returns nothing. Any pointers would be appreciated!



      Thanks!










      share|improve this question














      I'm trying to create a query that will accept input from a form (from a set of 3 radio buttons, one for "male" one for "female" and one for "either") and return all the entries of the specified input.



      I'm a bit of a newbie to MS Access so apologies if this is a super basic question. I've looked a lot online to find a solution but can't find one that works!



      The query I have so far is this:



         IIf([Forms]![AgeRangeQueryForm]![GenderOptionGroup]. 
      [Value]=1,"M",IIf([Forms]![AgeRangeQueryForm]![GenderOptionGroup].
      [Value]=2,"F",IIf([Forms]![AgeRangeQueryForm]![GenderOptionGroup].
      [Value]=3,([Child Details].[Gender])="M" Or ([Child Details].
      [Gender])="F")))


      If it helps the SQL code for the query is this:



         WHERE ((([Child Details].Gender)=IIf([Forms]![AgeRangeQueryForm]! 
      [GenderOptionGroup].[Value]=1,"M",IIf([Forms]![AgeRangeQueryForm]!
      [GenderOptionGroup].[Value]=2,"F",IIf([Forms]![AgeRangeQueryForm]!
      [GenderOptionGroup].[Value]=3,([Child Details].[Gender])="M" Or ([Child
      Details].[Gender])="F"))))


      I can make this work for when Male and Female are selected, but unfortunately I can't get it to work if I select either. In this case it should be just returning all entries regardless of what gender is in the field but it just returns nothing. Any pointers would be appreciated!



      Thanks!







      database ms-access






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 3 at 17:58









      YoddlenodYoddlenod

      193




      193
























          1 Answer
          1






          active

          oldest

          votes


















          2














          The issue with your code is that if the final option is selected, the resulting WHERE clause would read:



          where [Child Details].Gender = ([Child Details].[Gender]="M" or [Child Details].[Gender]="F")


          And since [Child Details].Gender is string-valued, it will never equal the boolean value returned by the bracketed expression.





          There are multiple ways to achieve this: one possible method is using a combination of the like operator and the choose function:



          where 
          [child details].gender like
          choose([Forms]![AgeRangeQueryForm]![GenderOptionGroup],"M","F","[MF]")


          Alternatively, you could use use the switch function in a similar way:



          where 
          [child details].gender like
          switch
          (
          [Forms]![AgeRangeQueryForm]![GenderOptionGroup]=1,"M",
          [Forms]![AgeRangeQueryForm]![GenderOptionGroup]=2,"F",
          [Forms]![AgeRangeQueryForm]![GenderOptionGroup]=3,"[MF]"
          )


          Or, just using boolean logic:



          where 
          ([Forms]![AgeRangeQueryForm]![GenderOptionGroup] in (1,3) and [child details].gender = "M") or
          ([Forms]![AgeRangeQueryForm]![GenderOptionGroup] in (2,3) and [child details].gender = "F")


          Or, if you don't wish to restrict the results returned when the third option is selected, precede all of the above filters with:



          ([Forms]![AgeRangeQueryForm]![GenderOptionGroup] = 3) or


          For example:



          where 
          ([Forms]![AgeRangeQueryForm]![GenderOptionGroup] = 3) or
          ([child details].gender = choose([Forms]![AgeRangeQueryForm]![GenderOptionGroup],"M","F"))





          share|improve this answer


























          • Unless there is a third gender (unknown?) then the either option could be simplified to return true and only apply a filter if a single gender is selected.

            – manotheshark
            Jan 3 at 18:51











          • @manotheshark I have updated my answer to reflect your suggestion, thanks.

            – Lee Mac
            Jan 3 at 18:56






          • 1





            Ah, I see! Thanks @LeeMac for your detailed response.

            – Yoddlenod
            Jan 7 at 12:14














          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54027454%2fcreating-a-query-in-ms-access-that-returns-all-entries-if-no-input-is-supplied-o%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          The issue with your code is that if the final option is selected, the resulting WHERE clause would read:



          where [Child Details].Gender = ([Child Details].[Gender]="M" or [Child Details].[Gender]="F")


          And since [Child Details].Gender is string-valued, it will never equal the boolean value returned by the bracketed expression.





          There are multiple ways to achieve this: one possible method is using a combination of the like operator and the choose function:



          where 
          [child details].gender like
          choose([Forms]![AgeRangeQueryForm]![GenderOptionGroup],"M","F","[MF]")


          Alternatively, you could use use the switch function in a similar way:



          where 
          [child details].gender like
          switch
          (
          [Forms]![AgeRangeQueryForm]![GenderOptionGroup]=1,"M",
          [Forms]![AgeRangeQueryForm]![GenderOptionGroup]=2,"F",
          [Forms]![AgeRangeQueryForm]![GenderOptionGroup]=3,"[MF]"
          )


          Or, just using boolean logic:



          where 
          ([Forms]![AgeRangeQueryForm]![GenderOptionGroup] in (1,3) and [child details].gender = "M") or
          ([Forms]![AgeRangeQueryForm]![GenderOptionGroup] in (2,3) and [child details].gender = "F")


          Or, if you don't wish to restrict the results returned when the third option is selected, precede all of the above filters with:



          ([Forms]![AgeRangeQueryForm]![GenderOptionGroup] = 3) or


          For example:



          where 
          ([Forms]![AgeRangeQueryForm]![GenderOptionGroup] = 3) or
          ([child details].gender = choose([Forms]![AgeRangeQueryForm]![GenderOptionGroup],"M","F"))





          share|improve this answer


























          • Unless there is a third gender (unknown?) then the either option could be simplified to return true and only apply a filter if a single gender is selected.

            – manotheshark
            Jan 3 at 18:51











          • @manotheshark I have updated my answer to reflect your suggestion, thanks.

            – Lee Mac
            Jan 3 at 18:56






          • 1





            Ah, I see! Thanks @LeeMac for your detailed response.

            – Yoddlenod
            Jan 7 at 12:14


















          2














          The issue with your code is that if the final option is selected, the resulting WHERE clause would read:



          where [Child Details].Gender = ([Child Details].[Gender]="M" or [Child Details].[Gender]="F")


          And since [Child Details].Gender is string-valued, it will never equal the boolean value returned by the bracketed expression.





          There are multiple ways to achieve this: one possible method is using a combination of the like operator and the choose function:



          where 
          [child details].gender like
          choose([Forms]![AgeRangeQueryForm]![GenderOptionGroup],"M","F","[MF]")


          Alternatively, you could use use the switch function in a similar way:



          where 
          [child details].gender like
          switch
          (
          [Forms]![AgeRangeQueryForm]![GenderOptionGroup]=1,"M",
          [Forms]![AgeRangeQueryForm]![GenderOptionGroup]=2,"F",
          [Forms]![AgeRangeQueryForm]![GenderOptionGroup]=3,"[MF]"
          )


          Or, just using boolean logic:



          where 
          ([Forms]![AgeRangeQueryForm]![GenderOptionGroup] in (1,3) and [child details].gender = "M") or
          ([Forms]![AgeRangeQueryForm]![GenderOptionGroup] in (2,3) and [child details].gender = "F")


          Or, if you don't wish to restrict the results returned when the third option is selected, precede all of the above filters with:



          ([Forms]![AgeRangeQueryForm]![GenderOptionGroup] = 3) or


          For example:



          where 
          ([Forms]![AgeRangeQueryForm]![GenderOptionGroup] = 3) or
          ([child details].gender = choose([Forms]![AgeRangeQueryForm]![GenderOptionGroup],"M","F"))





          share|improve this answer


























          • Unless there is a third gender (unknown?) then the either option could be simplified to return true and only apply a filter if a single gender is selected.

            – manotheshark
            Jan 3 at 18:51











          • @manotheshark I have updated my answer to reflect your suggestion, thanks.

            – Lee Mac
            Jan 3 at 18:56






          • 1





            Ah, I see! Thanks @LeeMac for your detailed response.

            – Yoddlenod
            Jan 7 at 12:14
















          2












          2








          2







          The issue with your code is that if the final option is selected, the resulting WHERE clause would read:



          where [Child Details].Gender = ([Child Details].[Gender]="M" or [Child Details].[Gender]="F")


          And since [Child Details].Gender is string-valued, it will never equal the boolean value returned by the bracketed expression.





          There are multiple ways to achieve this: one possible method is using a combination of the like operator and the choose function:



          where 
          [child details].gender like
          choose([Forms]![AgeRangeQueryForm]![GenderOptionGroup],"M","F","[MF]")


          Alternatively, you could use use the switch function in a similar way:



          where 
          [child details].gender like
          switch
          (
          [Forms]![AgeRangeQueryForm]![GenderOptionGroup]=1,"M",
          [Forms]![AgeRangeQueryForm]![GenderOptionGroup]=2,"F",
          [Forms]![AgeRangeQueryForm]![GenderOptionGroup]=3,"[MF]"
          )


          Or, just using boolean logic:



          where 
          ([Forms]![AgeRangeQueryForm]![GenderOptionGroup] in (1,3) and [child details].gender = "M") or
          ([Forms]![AgeRangeQueryForm]![GenderOptionGroup] in (2,3) and [child details].gender = "F")


          Or, if you don't wish to restrict the results returned when the third option is selected, precede all of the above filters with:



          ([Forms]![AgeRangeQueryForm]![GenderOptionGroup] = 3) or


          For example:



          where 
          ([Forms]![AgeRangeQueryForm]![GenderOptionGroup] = 3) or
          ([child details].gender = choose([Forms]![AgeRangeQueryForm]![GenderOptionGroup],"M","F"))





          share|improve this answer















          The issue with your code is that if the final option is selected, the resulting WHERE clause would read:



          where [Child Details].Gender = ([Child Details].[Gender]="M" or [Child Details].[Gender]="F")


          And since [Child Details].Gender is string-valued, it will never equal the boolean value returned by the bracketed expression.





          There are multiple ways to achieve this: one possible method is using a combination of the like operator and the choose function:



          where 
          [child details].gender like
          choose([Forms]![AgeRangeQueryForm]![GenderOptionGroup],"M","F","[MF]")


          Alternatively, you could use use the switch function in a similar way:



          where 
          [child details].gender like
          switch
          (
          [Forms]![AgeRangeQueryForm]![GenderOptionGroup]=1,"M",
          [Forms]![AgeRangeQueryForm]![GenderOptionGroup]=2,"F",
          [Forms]![AgeRangeQueryForm]![GenderOptionGroup]=3,"[MF]"
          )


          Or, just using boolean logic:



          where 
          ([Forms]![AgeRangeQueryForm]![GenderOptionGroup] in (1,3) and [child details].gender = "M") or
          ([Forms]![AgeRangeQueryForm]![GenderOptionGroup] in (2,3) and [child details].gender = "F")


          Or, if you don't wish to restrict the results returned when the third option is selected, precede all of the above filters with:



          ([Forms]![AgeRangeQueryForm]![GenderOptionGroup] = 3) or


          For example:



          where 
          ([Forms]![AgeRangeQueryForm]![GenderOptionGroup] = 3) or
          ([child details].gender = choose([Forms]![AgeRangeQueryForm]![GenderOptionGroup],"M","F"))






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 3 at 18:55

























          answered Jan 3 at 18:33









          Lee MacLee Mac

          5,76541644




          5,76541644













          • Unless there is a third gender (unknown?) then the either option could be simplified to return true and only apply a filter if a single gender is selected.

            – manotheshark
            Jan 3 at 18:51











          • @manotheshark I have updated my answer to reflect your suggestion, thanks.

            – Lee Mac
            Jan 3 at 18:56






          • 1





            Ah, I see! Thanks @LeeMac for your detailed response.

            – Yoddlenod
            Jan 7 at 12:14





















          • Unless there is a third gender (unknown?) then the either option could be simplified to return true and only apply a filter if a single gender is selected.

            – manotheshark
            Jan 3 at 18:51











          • @manotheshark I have updated my answer to reflect your suggestion, thanks.

            – Lee Mac
            Jan 3 at 18:56






          • 1





            Ah, I see! Thanks @LeeMac for your detailed response.

            – Yoddlenod
            Jan 7 at 12:14



















          Unless there is a third gender (unknown?) then the either option could be simplified to return true and only apply a filter if a single gender is selected.

          – manotheshark
          Jan 3 at 18:51





          Unless there is a third gender (unknown?) then the either option could be simplified to return true and only apply a filter if a single gender is selected.

          – manotheshark
          Jan 3 at 18:51













          @manotheshark I have updated my answer to reflect your suggestion, thanks.

          – Lee Mac
          Jan 3 at 18:56





          @manotheshark I have updated my answer to reflect your suggestion, thanks.

          – Lee Mac
          Jan 3 at 18:56




          1




          1





          Ah, I see! Thanks @LeeMac for your detailed response.

          – Yoddlenod
          Jan 7 at 12:14







          Ah, I see! Thanks @LeeMac for your detailed response.

          – Yoddlenod
          Jan 7 at 12:14






















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54027454%2fcreating-a-query-in-ms-access-that-returns-all-entries-if-no-input-is-supplied-o%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Mossoró

          Error while reading .h5 file using the rhdf5 package in R

          Pushsharp Apns notification error: 'InvalidToken'