Results 1 to 8 of 8
  1. #1
    dgmdvm is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Mar 2014
    Posts
    84

    Parameters in query expressions

    I have a cross tab query that has an expression in a value field. The expression includes a Dcount calculation from a different query. The main query returns records based on a range of dates so there are date parameters in the main query. The entire cross tab query works correctly when I run it. What I would like to do however, is filter the results in the cross tab query AND in the query that lies inside the expression based on a parameter that will limit the results to either persons that are senior citizens or persons that are veterans. In the cross tab query, I have added row headings for senior citizens and veterans. I have VBA code in a procedure that calls the report that will filter on one of these two parameters. What I don't know how to do is to put the same filter on the query that is inside the expression of the cross tab query. My goal here is to use the same report container, and the same query container for both sets of reports. I know that I could set up separate queries for all of this but I was trying to be economical with my code as well as my queries. Is this even possible?



    The following is the procedure that calls the report. It has multiple case statements but I removed most of them since they don't apply to my question. Case 9 returns a report of all individuals. Case 11 and Case 12 return reports that are filtered to return only veterans or senior citizens. The same report is used for all 3 Case statements. The On load event of the report declares which query is used to populate the report. That On load event is not listed here as it works correctly.

    Code:
    Private Sub CmdReport_Click()
      On Error GoTo Err_CmdReport_Click
        
      Dim startdate As Date
      Dim enddate As Date
      Dim DateRange As String
      Dim rptlabel As String
      
      If GetPrinter() = False Then
             Msgbox " Printers must be set up in the print settings menu before printing reports.", vbOKOnly, "Need Printer setup"
             Exit Sub
      ElseIf IsNull(Me.Text2.Value) Then
             Msgbox " Please enter the range of dates for the report", vbOKOnly, "Missing dates"
             Me.Text2.SetFocus
             Exit Sub
      ElseIf IsNull(Me.Text4.Value) Then
             Msgbox " Please enter the range of dates for the report", vbOKOnly, "Missing dates"
             Me.Text4.SetFocus
             Exit Sub
      
        
      End If
         If Frame89.Value = 7 Then
             DoCmd.OpenForm "Client_List_Reports", acNormal, , , acFormEdit, acWindowNormal, DateRange
         End If
      startdate = Me.Text2.Value
      enddate = Me.Text4.Value
      DateRange = "tblServiceInstance.Indate >= #" & startdate & "# AND tblServiceInstance.Indate <= #" & enddate & "#"
     
     
         
         Select Case Me.Frame89.Value
    
    
         Case 9
         rptname = "IndividualsAssistedbyCounty_Report"
         prnttype = "Document"
         qryname = "qryIndividualsAssisted_Crosstab"
         rptlabel = "Individuals"
         If RecordCount(qryname) = 0 Then
            Msgbox "There was no activity for this range of dates", vbOKOnly
            Exit Sub
         End If
    
         DoCmd.OpenReport rptname, acViewReport, , , acWindowNormal, rptlabel
         Set rpt = Reports(rptname)
         Set rpt.Printer = Application.Printers(Selprnt(prnttype))
         rpt.Printer.Orientation = acPRORPortrait
        
         
         Case 11
         rptname = "IndividualsAssistedbyCounty_Report"
         prnttype = "Document"
         qryname = "qryIndividualsSpecialAssisted_Crosstab"
          rptlabel = "Veteran"
         If RecordCount(qryname) = 0 Then
            Msgbox "There was no activity for this range of dates", vbOKOnly
            Exit Sub
         End If
    
         DoCmd.OpenReport rptname, acViewReport, , "Cl_Veteran = True ", acWindowNormal, rptlabel
         Set rpt = Reports(rptname)
         Set rpt.Printer = Application.Printers(Selprnt(prnttype))
         rpt.Printer.Orientation = acPRORPortrait
         
    
         Case 12
         rptname = "IndividualsAssistedbyCounty_Report"
         prnttype = "Document"
         qryname = "qryIndividualsSpecialAssisted_Crosstab"
         rptlabel = "Senior Citizen"
         If RecordCount(qryname) = 0 Then
            Msgbox "There was no activity for this range of dates", vbOKOnly
            Exit Sub
         End If
    
         DoCmd.OpenReport rptname, acViewReport, , "Cl_ageType= 'Senior Citizen' ", acWindowNormal, rptlabel
         Set rpt = Reports(rptname)
         Set rpt.Printer = Application.Printers(Selprnt(prnttype))
         rpt.Printer.Orientation = acPRORPortrait
        
         
        
    Case Else    'no option buttons have been selected
    
           Msgbox "You must select at least one option to generate a report"
    
        End Select
    
        
    Set rpt = Nothing
    Application.Printer = Nothing
    
    Exit_Err_CmdReport_Click:
      Exit Sub
    Here is the edit view of the query used to filter by senior citizens only. In the expression field, I have spelled out that the underlying query "qryFurnitureOnHoldSuummary" filter on the [CL_agetype] field. That I am trying to do is replace that explicit criteria with a parameter that I can declare in the VBA code. I highlighted the criteria that I want to replace with a parameter. My question is: Is this possible? How do I do it?

    On a related note. I have noticed that when I open a query in design view one table is highlighted in yellow. All my queries are opening this way. What is the significance of the yellow box. I never noticed that before.

    Click image for larger version. 

Name:	query.png 
Views:	26 
Size:	53.3 KB 
ID:	41542

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    user can fill controls on the form, then the code builds the 'where' clause for the base query.
    the Xtab query runs off it.

    'alter the query with your criteria....
    Code:
    '----------------
    sub btnOpenQry_click()
    '----------------
    dim sWhere as string 
    dim qdf as querydef
    sWhere = " where 1=1"
    if not IsNUll(cboST) then sWhere = sWhere & " and [State]='" & cboST & "'"
    if not IsNUll(cboCity) then sWhere = sWhere & " and [city]='" & cboCity & "'"
    if not IsNUll(cboZip) then sWhere = sWhere & " and [ZipCode]='" & cboZip & "'"
    
    set qdf = currentdb.querydefs("qsMyQry")
    qdf.sql = "Select * from table " & swhere
    qdf.close
    docmd.openquery "qxCrosstab"
    end sub

  3. #3
    dgmdvm is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Mar 2014
    Posts
    84
    ???
    I don't think you answer applies to my question. Wrong thread?

  4. #4
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    No, i was fixing any problem in the base query via forms. If you want that way.

  5. #5
    dgmdvm is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Mar 2014
    Posts
    84
    The base query is not a problem, it works fine. I chose in this instance not to use querydefs. If I need a querydef to fix my problem with the expression field I will do so. My question remains, can you change the criteria of the Dcount expression in the crosstab query by using a parameter? If so, How does one accomplish it?

  6. #6
    Micron is online now Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,422
    What is the significance of the yellow box. I never noticed that before.
    Apparently is some sort of new feature in query grid and relationships window where you can 'highlight' several tables and move them as a group. Hasn't happened to me yet because I've turned off updates. Got tired of M$ "improvements"

    As for your remaining question, I suppose you could define a query parameter (right click on query background) so that you get value(s) from unbound form controls that your existing code populates. Normally if I was coding all of this I wouldn't bother with a query as ranman seems to be saying. Might as well do it all from within a procedure.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    dgmdvm is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Mar 2014
    Posts
    84
    As for your remaining question, I suppose you could define a query parameter (right click on query background) so that you get value(s) from unbound form controls that your existing code populates
    .

    I created an invisible text box in my form , used VBA to enter a value equal to the entire criteria string, created a parameter and placed the parameter in my expression. It does work. I do agree it is a bit clunky.

    Might as well do it all from within a procedure.
    By this you mean creating a dynamic query for the report?

    Thanks for the help.

  8. #8
    Micron is online now Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,422
    By this you mean creating a dynamic query for the report?
    That is a common approach. Usually requires that you deal with the possibilities, such as one or more controls not having a value, that a before value isn't greater than an end value - that sort of thing. Often the WHERE part of vba generated sql is just concatenated to the first part(s) so that the entire thing is made up of either the first part alone (if no criteria) or together with the criteria if those controls have values.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Expressions and Functions in Query Not Working
    By ItsRoland in forum Queries
    Replies: 6
    Last Post: 07-10-2018, 10:29 AM
  2. Object Dependency for Expressions in Query?
    By accessnewbie352 in forum Queries
    Replies: 2
    Last Post: 01-09-2015, 09:03 PM
  3. Using Expressions as Field Names in a Query
    By kevinegg45 in forum Queries
    Replies: 3
    Last Post: 06-26-2013, 06:22 AM
  4. Replies: 4
    Last Post: 02-12-2013, 12:54 PM
  5. Query Expressions
    By Andrea in forum Queries
    Replies: 6
    Last Post: 03-03-2011, 03:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums