Setting a parameter for a report based on a query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • karanj
    New Member
    • Jun 2007
    • 3

    Setting a parameter for a report based on a query

    Hi all,

    I'm facing an issue with Access that I haven't been able to find a solution for, despite hours of googling, so I hope someone here can help!

    I've got a report - rptClientsByCou ntry - which is based on a query - qryClientsByCou ntry - that takes a parameter, myCountry, to filter the results. I need to be able to generate this report depending on the user's choice of country, preferably from a drop-down list.

    Leaving aside for a minute the form part, I can't figure out how to pass a string to the report such that it accepts it as the parameter to the underlying query. Psuedo code would be:

    sub command_click()

    get current value of country drop down

    set parameter myCountry on report

    DoCmd.OpenRepor t "rptClientsByCo untry", acViewPreview

    end sub

    Can this be done? Or must I resort to setting the Where condition on the DoCmd.OpenRepor t?

    Thanks in advance!
  • maxamis4
    Recognized Expert Contributor
    • Jan 2007
    • 295

    #2
    It works just like a form

    Code:
        stDocName = "frm Name"
        
        stLinkCriteria = "[Report Criteria]=" & Me![form criteria]
        DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
    Last edited by maxamis4; Jun 6 '07, 05:34 AM. Reason: forgot a pieace of code

    Comment

    • jamjar
      New Member
      • Apr 2007
      • 50

      #3
      Originally posted by maxamis4
      It works just like a form

      Code:
          stDocName = "frm Name"
          
          stLinkCriteria = "[Report Criteria]=" & Me![form criteria]
          DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
      This method implies you get rid of the parameters per se.
      You can pass parameters to the query in code, but I haven't figured out how to use these to run a Select query. For an action query something like this works:
      Code:
        Dim qdf As QueryDef
      	Set qdf = CurrentDb.QueryDefs("yourQuery")
      	qdf.Parameters("myCountry").Value = Me.cmbCountry
      	qdf.Execute
      	qdf.Close
      	Set qdf = Nothing
      but if you try to Execute a select query it doesn't work. Ditto if you replace the execute with DoCmd.OpenRepor t you still receive a prompt for the parameter.
      So like karanj I'm curious if anyone knows if/how you can pass a parameter to a select query so the user does not receive the prompt?

      James

      Comment

      • karanj
        New Member
        • Jun 2007
        • 3

        #4
        Originally posted by jamjar
        This method implies you get rid of the parameters per se.
        ...
        if you replace the execute with DoCmd.OpenRepor t you still receive a prompt for the parameter.
        So like karanj I'm curious if anyone knows if/how you can pass a parameter to a select query so the user does not receive the prompt?
        Yep, exactly - this kind of basic modularisation would be very helpful! The "official" solution that I've found however is a little disheartening - see this article on the office.microsof t.com site. Surely that means that the query-report-form will effectively be coupled quite tightly?

        Comment

        • karanj
          New Member
          • Jun 2007
          • 3

          #5
          Originally posted by maxamis4
          It works just like a form

          Code:
              stDocName = "frm Name"
              
              stLinkCriteria = "[Report Criteria]=" & Me![form criteria]
              DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
          here effectively the form has to "know" the table/sql - I'd prefer to avoid this if possible so that the queries can be built by one person and the form/report by another. This seems to me something rather straightforward ...

          Comment

          • jamjar
            New Member
            • Apr 2007
            • 50

            #6
            Originally posted by karanj
            Yep, exactly - this kind of basic modularisation would be very helpful! The "official" solution that I've found however is a little disheartening - see this article on the office.microsof t.com site. Surely that means that the query-report-form will effectively be coupled quite tightly?
            I would think so. The query would only be useful for the one form. I guess that means whatever form you may be starting with, you need to call the query's parameter form up before running the report based on it, and populate the parameter form with any selections that may already have been made in the originating form?

            James

            Comment

            • maxamis4
              Recognized Expert Contributor
              • Jan 2007
              • 295

              #7
              Let it stand that stlinkcriteria can be defined to what ever you want

              here is an example

              Code:
                     stLinkCriteria = "[fID]=" & Me![fID]
              as well you could use a recordset

              Comment

              • jamjar
                New Member
                • Apr 2007
                • 50

                #8
                Originally posted by maxamis4
                Let it stand that stlinkcriteria can be defined to what ever you want

                here is an example

                Code:
                       stLinkCriteria = "[fID]=" & Me![fID]
                Agreed.
                There have been times when I have wished to be able to use parameters instead, but I can't remember a good example.

                Comment

                • cfs
                  New Member
                  • Feb 2008
                  • 1

                  #9
                  One sulution I think:

                  Create an invisible text-field in the active form (assuming you use a form to invoke the VBA-code, or have an active form that stays open). Use the VBA-code to write the desired parameter to the text-field (be sure to to this the right place regarding loops etc).

                  In the query, assign the parameter in the query to the invisible text-field. As you selection changes, so does the text-field, and the query based upon it shoul return the right results. at least this worked for me :)

                  Comment

                  • truthlover
                    New Member
                    • Dec 2007
                    • 107

                    #10
                    Originally posted by maxamis4
                    It works just like a form

                    Code:
                     
                    stDocName = "frm Name"
                     
                    stLinkCriteria = "[Report Criteria]=" & Me![form criteria]
                    DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
                    What am I doing wrong? This is my modification of the code, but I'm getting a "Compile Error: Variable not defined" for both the stDocName and stLinkCriteria.
                    Code:
                    Private Sub cmdPrintSelected_Click()
                    	stDocName = "rpt_SurveyWorkOrder"
                     
                    		stLinkCriteria = "[tbl_SurveyWorkOrder.SurveyWorkOrderID]=" _
                    		& Me![tbl_SurveyWorkOrder.SurveyWorkOrderID]
                    		DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
                    End Sub
                    Sorry if this is a dumb question, I'm only just learning VBA

                    Thanks!

                    Comment

                    • truthlover
                      New Member
                      • Dec 2007
                      • 107

                      #11
                      Not sure if anyone saw this since it was the holiday weekend:
                      Originally posted by truthlover
                      What am I doing wrong? This is my modification of the code, but I'm getting a "Compile Error: Variable not defined" for both the stDocName and stLinkCriteria.
                      Code:
                      Private Sub cmdPrintSelected_Click()
                      	stDocName = "rpt_SurveyWorkOrder"
                       
                      		stLinkCriteria = "[tbl_SurveyWorkOrder.SurveyWorkOrderID]=" _
                      		& Me![tbl_SurveyWorkOrder.SurveyWorkOrderID]
                      		DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
                      End Sub
                      Sorry if this is a dumb question, I'm only just learning VBA

                      Thanks!

                      Comment

                      • maxamis4
                        Recognized Expert Contributor
                        • Jan 2007
                        • 295

                        #12
                        Not sure if you ever solved this but it looks like you are seetin your survey ID equal to the table. Which really has no reference. What you would need to do is set your report workid field what ever the name equal to the form survey id number.

                        Example

                        stLinkCriteria = "[Report.SurveyWo rkOrderID]=" _
                        & Me![tbl_SurveyWorkO rder.SurveyWork OrderID]

                        Comment

                        • truthlover
                          New Member
                          • Dec 2007
                          • 107

                          #13
                          Yes, I did get help on that. Thanks for replying!

                          Originally posted by maxamis4
                          Not sure if you ever solved this but it looks like you are seetin your survey ID equal to the table. Which really has no reference. What you would need to do is set your report workid field what ever the name equal to the form survey id number.

                          Example

                          stLinkCriteria = "[Report.SurveyWo rkOrderID]=" _
                          & Me![tbl_SurveyWorkO rder.SurveyWork OrderID]

                          Comment

                          Working...