Parameter Queries

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32653

    Parameter Queries

    I would like to open a parameter query so that it's visible to the operator (not as a recordset in the VBA code) but I also want to set the parameters before it's opened so that I can control what the operator sees when it's run from a form, but so that it's also possible to run the query natively (double-click the querydef object from the database window) and have it ask for the parameters.

    Is this possible?

    PS. Please request clarification if anything is not clear.
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32653

    #2
    An example of a parameter query (for illustration) would be :
    [CODE=SQL]PARAMETERS [Branch] Long, [Start Time] DateTime;
    SELECT [Sales_Order_Nbr] AS OrderNo,
    [TIME_PICKING_NO TE_PRINTED] AS Printed
    FROM Tran_PickPack
    WHERE [BRANCH_CODE]=Format([Branch],'00')
    AND [DATE_PICKING_NO TE_PRINTED]=Date()
    AND [TIME_PICKING_NO TE_PRINTED]>Format([Start Time],'Short Time')
    ORDER BY TIME_PICKING_NO TE_PRINTED,
    [Sales_Order_Nbr][/CODE]

    Comment

    • MMcCarthy
      Recognized Expert MVP
      • Aug 2006
      • 14387

      #3
      Try this ...


      [CODE=SQL]
      PARAMETERS [EnterBranch:] As Branch Long, [Enter Start Time:] As StartTime DateTime;
      SELECT [Sales_Order_Nbr] AS OrderNo,
      [TIME_PICKING_NO TE_PRINTED] AS Printed
      FROM Tran_PickPack
      WHERE [BRANCH_CODE]=Format([Branch],'00')
      AND [DATE_PICKING_NO TE_PRINTED]=Date()
      AND [TIME_PICKING_NO TE_PRINTED]>Format([StartTime],'Short Time')
      ORDER BY TIME_PICKING_NO TE_PRINTED,
      [Sales_Order_Nbr][/CODE]

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32653

        #4
        That's useful to know Mary (about renaming parameters), but I'm really after supplying the values in code that I already know, before then opening the PARAMETER SELECT query for full interaction with the operator.

        I can set a PARAMETER value in code by using the :
        Code:
        qdf(ParmName) = Value
        ...syntax, but that only allows programmatic access to the resultant data. I want it in a query grid as the operator is used to using when opening the query themselves.

        PS. This may be entirely unsupported in Access. If so then I must look at alternative approaches of course.

        Comment

        • MMcCarthy
          Recognized Expert MVP
          • Aug 2006
          • 14387

          #5
          Where are you getting the values from?

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32653

            #6
            I'm not yet, but I was planning to set up an unbound form for them.

            I didn't go there as I was trying to avoid any comments along the lines of "Put a reference to the form controls in place of the parameters." which would tie the query down to working only from the form.

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32653

              #7
              I'm particularly interested in the concept of doing this as, once done, I will have more scope to design simpler queries rather than always having to turn results out as reports. Thereby reducing development times.

              I appreciate this idea would not work in all environments, but my office is somewhere I can provide some basic queries for my IT colleagues to run competently where necessary.

              The more they can handle independently, the less involved I need to be with executing noddy queries.

              Comment

              • MMcCarthy
                Recognized Expert MVP
                • Aug 2006
                • 14387

                #8
                Originally posted by NeoPa
                I'm not yet, but I was planning to set up an unbound form for them.

                I didn't go there as I was trying to avoid any comments along the lines of "Put a reference to the form controls in place of the parameters." which would tie the query down to working only from the form.
                Not sure if I'm fully following what you are trying to do but you can use "User Defined" funtions in a query. Therefore you could design a function to return for instance the Long value of branch depending on whatever criteria you set up and call the function as follows:

                Parametre getBranch() As Branch Long

                I have never tried to use a function in this way so I suggest you run a simple test function to try it out. You can use them in the criteria if you can't use them in the Parametres.

                Mary

                Comment

                • Denburt
                  Recognized Expert Top Contributor
                  • Mar 2007
                  • 1356

                  #9
                  Originally posted by NeoPa
                  I'm really after supplying the values in code that I already know, before then opening the PARAMETER SELECT query for full interaction with the operator.
                  Couldn't you just build the SQL in code as they fill in the forms info then they could click to see the query in it's design state then when they close the form it replaces the actual values with the parameters?

                  Comment

                  • NeoPa
                    Recognized Expert Moderator MVP
                    • Oct 2006
                    • 32653

                    #10
                    I'm trying to make it easier and less complicated Denburt. I don't want users ever to have to redesign the query when using it (or even be able to really). Thanks for the thoughts though.

                    Comment

                    • MMcCarthy
                      Recognized Expert MVP
                      • Aug 2006
                      • 14387

                      #11
                      OK Try this ...

                      Using this SQL
                      [CODE=SQL]
                      PARAMETERS [EnterBranch:] As Branch Long, [Enter Start Time:] As StartTime DateTime;
                      SELECT [Sales_Order_Nbr] AS OrderNo,
                      [TIME_PICKING_NO TE_PRINTED] AS Printed
                      FROM Tran_PickPack
                      WHERE [BRANCH_CODE]=Format([Branch],'00')
                      AND [DATE_PICKING_NO TE_PRINTED]=Date()
                      AND [TIME_PICKING_NO TE_PRINTED]>Format([StartTime],'Short Time')
                      ORDER BY TIME_PICKING_NO TE_PRINTED,
                      [Sales_Order_Nbr][/CODE]

                      with this code
                      [Code=vb]
                      Dim qdf As QueryDef

                      Set qdf = CurrentDB.Query Defs("Your Query Name")
                      qdf.Parameters( "Branch") = 3
                      qdf.Parameters( "StartTime" ) = Now()
                      qdf.Execute "Your Query Name"
                      [/code]

                      Comment

                      • NeoPa
                        Recognized Expert Moderator MVP
                        • Oct 2006
                        • 32653

                        #12
                        Everything works fine until the qdf.Execute statement.
                        Originally posted by Access Help
                        Execute Method

                        Runs an action query or executes an SQL statement on a specified Connection or Database object.
                        But...
                        Originally posted by Access Help
                        Remarks

                        The Execute method is valid only for action queries. If you use Execute with another type of query, an error occurs.

                        Comment

                        • MMcCarthy
                          Recognized Expert MVP
                          • Aug 2006
                          • 14387

                          #13
                          Originally posted by NeoPa
                          Everything works fine until the qdf.Execute statement.

                          But...
                          I'll do some more research :)

                          However, what happens if you try to open the query as normal instead of executing it?

                          Comment

                          • MMcCarthy
                            Recognized Expert MVP
                            • Aug 2006
                            • 14387

                            #14
                            Originally posted by msquared
                            I'll do some more research :)

                            However, what happens if you try to open the query as normal instead of executing it?
                            OK based on further research it looks like you can't pass parameters in this manner except to an action query. For a select query it seems you can only return a recordset.

                            replace qdf.Execute "Query Name" with qdf.OpenRecords et("QueryName" )

                            You could then do a workaround and open a datasheet form based on the recordset rather than the query.

                            Comment

                            • ADezii
                              Recognized Expert Expert
                              • Apr 2006
                              • 8834

                              #15
                              Originally posted by NeoPa
                              I would like to open a parameter query so that it's visible to the operator (not as a recordset in the VBA code) but I also want to set the parameters before it's opened so that I can control what the operator sees when it's run from a form, but so that it's also possible to run the query natively (double-click the querydef object from the database window) and have it ask for the parameters.

                              Is this possible?

                              PS. Please request clarification if anything is not clear.
                              Comes with no guarantee whatsoever:
                              [CODE=sql]
                              PARAMETERS [Branch] Long, [Start Time] DateTime;
                              SELECT [Sales_Order_Nbr] AS OrderNo,
                              [Branch] As Param1, [Start Time] As Param2,
                              [TIME_PICKING_NO TE_PRINTED] AS Printed
                              FROM Tran_PickPack
                              WHERE [BRANCH_CODE]=Format([Branch],'00')
                              AND [DATE_PICKING_NO TE_PRINTED]=Date()
                              AND [TIME_PICKING_NO TE_PRINTED]>Format([Start Time],'Short Time')
                              ORDER BY TIME_PICKING_NO TE_PRINTED,
                              [Sales_Order_Nbr]
                              [/CODE]

                              Comment

                              Working...