Results 1 to 10 of 10
  1. #1
    Tom Evans is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2023
    Posts
    11

    Putting the date used in a Query into a Header Field of a Report

    I need help please. I am designing a Goods Dispatched Report which is based on a Query which retrieves the information from 2 Tables based on a date period e.g. 27 September to 26 October.


    The Query on the Date field looks like this “Between [Enter Start Date] and [Enter End Date]”.
    My problem is that I want the “Start Date” and the “End Date” to appear in the Header section of the report. Can this be done? If so, how?

  2. #2
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    566
    Quote Originally Posted by Tom Evans View Post
    I need help please. I am designing a Goods Dispatched Report which is based on a Query which retrieves the information from 2 Tables based on a date period e.g. 27 September to 26 October.
    The Query on the Date field looks like this “Between [Enter Start Date] and [Enter End Date]”.
    My problem is that I want the “Start Date” and the “End Date” to appear in the Header section of the report. Can this be done? If so, how?
    Remove the parameters from the query, and then move the filter to the Open event of the report. Then create an unbound form and put two unbound controls on it, one for each date. (so format them as dates). Then you can just add a button to your form that opens the report,

    DoCmd.OpenReport "someReport, Between Forms!frmUnboundParamForm![StartDate] AND Forms!frmUnboundParamForm![EndDate]

    Oh, right... putting the values in the header... two unbound textboxes one =Forms!frmUnboundParamForm![StartDate] and the other points to the other parameter.

  3. #3
    Tom Evans is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2023
    Posts
    11

    Thanks a million. Much appreciated.

    Quote Originally Posted by madpiet View Post
    Remove the parameters from the query, and then move the filter to the Open event of the report. Then create an unbound form and put two unbound controls on it, one for each date. (so format them as dates). Then you can just add a button to your form that opens the report,

    DoCmd.OpenReport "someReport, Between Forms!frmUnboundParamForm![StartDate] AND Forms!frmUnboundParamForm![EndDate]

    Oh, right... putting the values in the header... two unbound textboxes one =Forms!frmUnboundParamForm![StartDate] and the other points to the other parameter.
    Thanks ever so much. Will try it today.

  4. #4
    Tom Evans is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2023
    Posts
    11

    Confused

    Quote Originally Posted by Tom Evans View Post
    Thanks ever so much. Will try it today.
    Hello Madpiet. Please explain a little bit more what "moving the filter to the Open event of the Report" means.
    Thanks

  5. #5
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    566
    Tom,
    first the easy part: showing the date range of the filter on the report:

    Put a text box in the header of the report.
    Control Source of the textbox:

    Code:
    ="FROM: " & Format([Forms]![frmFilterCostcoData]![cboStartDate],"yyyy-mmm-dd") & " TO: " & Format([Forms]![frmFilterCostcoData]![cboEndDate],"yyyy-mmm-dd")
    When the report runs, it will grab the values you pass from the frmFilterCostcoData form controls and the report will only contain the records that fall between the two dates, cboStartDate and cboEndDate.

    Filtering the report...

    Instead of a hardcoded filter in the query that "gathers" the data for your report, you pass that same WHERE clause (minus the WHERE keyword) in the Open event of the report.

    Both queries and reports have filters (basically a WHERE clause, except in a report, you leave off the "WHERE" keyword). So for filters you *always* want applied to the report, you usually put those in the WHERE clause of the query you base the report on. If you want to apply additional filters to the report when you open the report, you can also pass a filter to the report, which filters the report data source a second time (the first is in the query's WHERE clause).

    The most flexible way to use a report is to leave the filtering out of the report's data source (the query the report is based on) and then pass a filter in the report's open event in VBA, like this:

    Code:
    Private Sub Command5_Click()    Dim strWhere As String
        strWhere = "[InvoiceDate] >= #" & Me.cboStartDate & "# AND [InvoiceDate] <= #" & Me.cboEndDate & "#"
        
        DoCmd.OpenReport "dbo_CostcoData", acViewReport, , strWhere, acWindowNormal
    End Sub
    I could add other filters, but basically what's happening is that the report is evaluating the contents of the two comboboxes cboStartDate and cboEndDate and passing that filter to the report when it opens (that's what "strWhere" is doing in the OpenReport command).

    You don't have to use just one or the other. If you always want the report to have a given static filter, you could include that in the WHERE clause of the query the report is based on, and then add more filters in the code, and the two sets of filters are just basically ANDed together.

    Hope that answers your question.

  6. #6
    Micron is online now Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    I might use one unbound control and concatenate, as in
    ="For the period between " & [StartDate] " and " & [EndDate]. That way, no matter the physical width of the date values you'll always end up with a well formed sentence. Otherwise, you'll have varying space width between your header words and dates. I used my own field names to illustrate that spaces in field names should be avoided.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    566
    (Thought I did that... sorry...was a bit of an afterthought).

  8. #8
    Micron is online now Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    If you did I missed it. I did see this part
    Then create an unbound form and put two unbound controls on it
    so I wasn't looking for a different suggestion about that.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #9
    Tom Evans is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2023
    Posts
    11
    Thanks Madpiet. I shouldn't have any further problems as you have explained it very well.
    Have a great day!

  10. #10
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    566
    You're welcome. Post back if you get stuck. There are lots of folks here who can help you out.

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

Similar Threads

  1. Report header and Page header Not printing
    By finlainret in forum Forms
    Replies: 1
    Last Post: 12-03-2022, 10:33 AM
  2. Replies: 6
    Last Post: 05-01-2017, 02:34 PM
  3. Replies: 1
    Last Post: 06-15-2016, 08:45 AM
  4. Replies: 8
    Last Post: 03-03-2013, 02:53 PM
  5. Replies: 32
    Last Post: 06-20-2011, 07:30 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