Applying a filter with a nested subform

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • angi35
    New Member
    • Jan 2008
    • 55

    Applying a filter with a nested subform

    Hi -

    In Access 2000, I have a form I want to filter, but I can't get the syntax right in the code.

    Form: [JOB filtered]
    Subform: [P PRODUCT]
    Control on [P PRODUCT]: txtStart
    Nested Subform on [P PRODUCT]: [Survey Form]
    Control on [Survey Form]: txtSDate

    Both controls are dates.

    I want to filter the form "On Open" to show only records in which txtStart is not null and txtSDate is null.

    I've figured out how to reference the controls:

    Forms![JOB filtered]![P PRODUCT].Form![txtStart]

    and

    Forms![JOB filtered]![P PRODUCT].Form![Survey Form].Form![txtSDate]

    ... but I can't figure out the null/not null part.

    How would I write this?

    Angi
  • puppydogbuddy
    Recognized Expert Top Contributor
    • May 2007
    • 1923

    #2
    Originally posted by angi35
    Hi -

    In Access 2000, I have a form I want to filter, but I can't get the syntax right in the code.

    Form: [JOB filtered]
    Subform: [P PRODUCT]
    Control on [P PRODUCT]: txtStart
    Nested Subform on [P PRODUCT]: [Survey Form]
    Control on [Survey Form]: txtSDate

    Both controls are dates.

    I want to filter the form "On Open" to show only records in which txtStart is not null and txtSDate is null.

    I've figured out how to reference the controls:

    Forms![JOB filtered]![P PRODUCT].Form![txtStart]

    and

    Forms![JOB filtered]![P PRODUCT].Form![Survey Form].Form![txtSDate]

    ... but I can't figure out the null/not null part.

    How would I write this?

    Angi
    How about this:
    Code:
    SELECT * FROM YourTable
    WHERE Not IsNull(Forms![JOB filtered]![P PRODUCT].Form![txtStart]) And IsNull(Forms![JOB filtered]![P PRODUCT].Form![Survey Form].Form![txtSDate])

    Comment

    • angi35
      New Member
      • Jan 2008
      • 55

      #3
      Originally posted by puppydogbuddy
      How about this:
      Code:
      SELECT * FROM YourTable
      WHERE Not IsNull(Forms![JOB filtered]![P PRODUCT].Form![txtStart]) And IsNull(Forms![JOB filtered]![P PRODUCT].Form![Survey Form].Form![txtSDate])
      I'm getting an error on line 1, highlighting the * and saying "Compile Error. Expected: Case"

      Also, this form is run from a query. Should I be inserting the query name instead of the table in line 1? (Incidentally, that didn't make a difference regarding the error message.)

      Comment

      • puppydogbuddy
        Recognized Expert Top Contributor
        • May 2007
        • 1923

        #4
        Originally posted by angi35
        I'm getting an error on line 1, highlighting the * and saying "Compile Error. Expected: Case"

        Also, this form is run from a query. Should I be inserting the query name instead of the table in line 1? (Incidentally, that didn't make a difference regarding the error message.)
        1. If the data source for this query is another query and not a table, then by all means, use the query name.
        2. Did you type this query in a continuous fashion with no line breaks and no extra spaces etc.? Please post your query so that I can look at it.

        Thanks.

        Comment

        • angi35
          New Member
          • Jan 2008
          • 55

          #5
          Originally posted by puppydogbuddy
          1. If the data source for this query is another query and not a table, then by all means, use the query name.
          2. Did you type this query in a continuous fashion with no line breaks and no extra spaces etc.? Please post your query so that I can look at it.

          Thanks.

          Oh... You're suggesting I create a query for the form instead of using VB code to filter it. I was trying to put your SQL code in the VB window. Hah...No wonder it didn't work.

          So, the syntax in my first message for identifying the controls on the subforms was in VB. Should the same syntax work in SQL? I'm thinking no -- it doesn't appear to work. It's asking me to enter a parameter value for Forms!JOB filtered!P

          Comment

          • puppydogbuddy
            Recognized Expert Top Contributor
            • May 2007
            • 1923

            #6
            Originally posted by angi35
            Oh... You're suggesting I create a query for the form instead of using VB code to filter it. I was trying to put your SQL code in the VB window. Hah...No wonder it didn't work.

            So, the syntax in my first message for identifying the controls on the subforms was in VB. Should the same syntax work in SQL? I'm thinking no -- it doesn't appear to work. It's asking me to enter a parameter value for Forms!JOB filtered!P
            It should also work in the vb code window, provided you have a semi-colon at the end of the sql string. When you use the Access sql view, Access will add the semi-colon for you. In the vb window, you have to add the semi-colon because the vb editor will not do it for you.

            As for the parameters, you are probably being asked to declare it. IF you are in query design view, go to the Access command menu and select query>parameter s and when the dialog box comes up, fill inhe particulars. If you are the code window, the parameters clause must precede your select statement. The syntax is as follows (fill in P;s datatype):

            PARAMETERS Forms!JOB filtered!P P's datatype;

            Comment

            • angi35
              New Member
              • Jan 2008
              • 55

              #7
              Originally posted by puppydogbuddy
              It should also work in the vb code window, provided you have a semi-colon at the end of the sql string. When you use the Access sql view, Access will add the semi-colon for you. In the vb window, you have to add the semi-colon because the vb editor will not do it for you.

              As for the parameters, you are probably being asked to declare it. IF you are in query design view, go to the Access command menu and select query>parameter s and when the dialog box comes up, fill inhe particulars. If you are the code window, the parameters clause must precede your select statement. The syntax is as follows (fill in P;s datatype):

              PARAMETERS Forms!JOB filtered!P P's datatype;

              The problem is that there is no "P". It's the subform [P Product]. But Access isn't recognizing the whole name, and it's not recognizing that it's a form. If it's looking for a datatype, I can't really give it one, since it isn't data.

              Comment

              • puppydogbuddy
                Recognized Expert Top Contributor
                • May 2007
                • 1923

                #8
                Originally posted by angi35
                The problem is that there is no "P". It's the subform [P Product]. But Access isn't recognizing the whole name, and it's not recognizing that it's a form. If it's looking for a datatype, I can't really give it one, since it isn't data.
                Ok, that is your problem, you are providing an invalid parameter....pr obably due to incorrect syntax.....whic h is why I asked you to post your current sql statement. In absence of your sql statement, I can only guess......your syntax problem could be anything from a missing parenthesis to an extra space.....pleas e post your current sql statement so that I can see it. Thanks.

                Comment

                • sierra7
                  Recognized Expert Contributor
                  • Sep 2007
                  • 446

                  #9
                  Hi

                  It seems to me that you are trying to filter a 'Main' form, using criteria set in the 'Sub-Form' and I didn't think that could be done in a straight forward way. (but always willing to learn!)

                  If I was to try this I would have thought that the underlying query on the Main JOB form would have involved joins to the tables holding the Null and NotNull dates, and those criteria would be hard coded in the Query Grid.

                  That way the Main form would open to show only JOBS meeting the criteria.

                  Depending on the nature of the data there may be multiple Main forms for the same JOB so the main query may have to Group by JOB; failing that you may have to rely on a correlated sub-query, but I'm pretty certain this is the path you will have to tread.

                  S7

                  Comment

                  • angi35
                    New Member
                    • Jan 2008
                    • 55

                    #10
                    Originally posted by sierra7
                    Hi

                    It seems to me that you are trying to filter a 'Main' form, using criteria set in the 'Sub-Form' and I didn't think that could be done in a straight forward way. (but always willing to learn!)

                    If I was to try this I would have thought that the underlying query on the Main JOB form would have involved joins to the tables holding the Null and NotNull dates, and those criteria would be hard coded in the Query Grid.

                    That way the Main form would open to show only JOBS meeting the criteria.

                    Depending on the nature of the data there may be multiple Main forms for the same JOB so the main query may have to Group by JOB; failing that you may have to rely on a correlated sub-query, but I'm pretty certain this is the path you will have to tread.

                    S7
                    Thanks Sierra7- yes, I created a new query for the new 'filtered' main form that linked the query behind the original main form and the tables behind the two subforms. It seems so obvious now that I've done it!

                    Angi

                    Comment

                    Working...