Use IF-THEN to change query parameters for a report

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Pookaroo85
    New Member
    • Nov 2013
    • 34

    Use IF-THEN to change query parameters for a report

    I have a form with a series of check boxes and combo boxes to change report criteria. Based on the user's selection, I need 1 - 17 reports to print with different criteria from the same 5 queries. Each query has been made into a sub-report. I cannot seem to figure out how to get the scenarios to influence the query parameters. For sake of space, I have only included 2 queries worth below.

    [Private Sub cmdPrint_Click( )
    Dim dbs As DAO.Database
    Dim qdf1 As DAO.QueryDef
    Dim qdf2 As DAO.QueryDef

    Dim stReport As String
    Set dbs = CurrentDb
    Set qdf1 = dbs.QueryDefs(" qrySlurryPumps_ Pump")
    Set qdf2 = dbs.QueryDefs(" qrySlurryPumps_ HandAdd")

    stReport = "rptPumpingReco rd"

    If Me.Check_L1 = True And Me.Check_L1HAS = False And Me.Check_L1HAP = False And Me.Check_L1OTC = True Then
    qdf1.Parameters ("PumpDate").Va lue = Forms![frmPrint]![ProductionDate]
    qdf1.Parameters ("SlurryTank.Va lue").Value = "Line 1"
    qdf1.Execute
    qdf1.Close

    qdf2.Parameters ("PumpDate").Va lue = Forms![frmPrint]![ProductionDate]
    qdf2.Parameters ("Hand_Add.Valu e").Value = "0"
    qdf2.Execute
    qdf2.Close
    DoCmd.OpenRepor t stReport, acViewPreview

    End If]
  • PhilOfWalton
    Recognized Expert Top Contributor
    • Mar 2016
    • 1430

    #2
    I suggest a totally different approach.

    Use the DoCmd.OpenRepor t method and build the WHERE filter from the criteria found in your form.

    Assuming the print button is on your form, you would have things like

    Code:
       Dim WhereClause as String
    
       WhereClause = "PumpDate = #" & ProductionDate & "#"
       WhereClause = WhereClause & " AND "SluryTankValue = " & Chr$(34) & "Line 1" & Chr$(34)
    
       DoCmd.OpenReport stReport, acViewPreview, , WhereClause
    Phil

    Comment

    • Pookaroo85
      New Member
      • Nov 2013
      • 34

      #3
      Would this work where I have to print the same report multiple times with different criteria based on the selections of the form?

      Comment

      • PhilOfWalton
        Recognized Expert Top Contributor
        • Mar 2016
        • 1430

        #4
        Yes, because you build different WHERE clauses depending on the information required in your reports. The report isn't "Created" until it has been supplied with the WHERE information.

        It won't work if you need different grouping & sorting in your reports though

        Phil

        Comment

        Working...