Results 1 to 7 of 7
  1. #1
    dcrqueens is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2015
    Posts
    3

    creating Formt o Enter Start & End Dates to View Report

    I have a form that I am trying to get to work with no success. Basicaly I have a query that a report is based off of. Instead of using the general prompt that comes up to run the query I want to create a form to enter the start and end date. I created the form using the instructions from http://allenbrowne.com/casu-08.html There are two boxes on the form txtStartDate and txtEndDate the form name is fromWhatDates. The query is called QryReviewStartEndDate and in the criteria for”Review Start Date” in my query I have >= [StartDate] < [EndDate] + 1. The report name is Rpt ReviewStartEndDate. In the header of the report I have two text boxes with the control source sent to =Forms.frmWhatDates.txtStartDate and =Forms.frmWhatDates.txtEndDate Once I enter the dates I have a botton to run the query with the following code in the On click event:

    On Error GoTo Err_Handler 'Remove the single quote from start of this line once you have it working.
    'Purpose: Filter a report to a date range.
    'Documentation: http://allenbrowne.com/casu-08.html
    'Note: Filter uses "less than the next day" in case the field has a time component.


    Dim strReport As String
    Dim strDateField As String
    Dim strWhere As String
    Dim lngView As Long
    Const strcJetDate = "\#mm\/dd\/yyyy\#" 'Do NOT change it to match your local settings.

    'DO set the values in the next 3 lines.
    strReport = "Rpt ReviewStartEndDate" 'Put your report name in these quotes.
    strDateField = "[Review Start Date]" 'Put your field name in the square brackets in these quotes.
    lngView = acViewPreview 'Use acViewNormal to print instead of preview.

    'Build the filter string.
    If IsDate(Me.txtStartDate) Then
    strWhere = "(" & strDateField & " >= " & Format(Me.txtStartDate, strcJetDate) & ")"
    End If
    If IsDate(Me.txtEndDate) Then
    If strWhere <> vbNullString Then
    strWhere = strWhere & " AND "
    End If
    strWhere = strWhere & "(" & strDateField & " < " & Format(Me.txtEndDate + 1, strcJetDate) & ")"
    End If

    'Close the report if already open: otherwise it won't filter properly.
    If CurrentProject.AllReports(strReport).IsLoaded Then
    DoCmd.Close acReport, strReport
    End If

    'Open the report.
    Debug.Print strWhere 'Remove the single quote from the start of this line for debugging purposes.
    DoCmd.OpenReport strReport, lngView, , strWhere

    Exit_Handler:
    Exit Sub

    Err_Handler:
    If Err.Number <> 2501 Then
    MsgBox "Error " & Err.Number & ": " & Err.Description, vbExclamation, "Cannot open report"
    End If
    Resume Exit_Handler

    After I enter the start and end date, then click the button to run the query I get the “Enter parameter Value” prompt for StartDate and EndDate.
    Does anyone have any ideas on how I can correct the problem?

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    To use that technique, you'd take the criteria out of the query. The code is doing the filtering.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    dcrqueens is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2015
    Posts
    3
    Hi phbaldy thank you for replying. I have tried to remove the code as well and it still gives me the pop ups to enter start and end dates.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Does the report open without prompts if you open it directly? If so, something is likely spelled wrong in the code. Can you post the db here?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    dcrqueens is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2015
    Posts
    3
    It is fixed thank you. Your question regarding the promps got me thinking. So I made a copy of the query that the report is using and gave it different name. Now it works. I dont know how that is bit it is fixed!!

  6. #6
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    pbaldy's already solved your problem but I'm curious why you wouldn't just use criteria in your query rather than filtering

    i.e. in the criteria of your query put


    between [forms]![fromWhatDates]![txtStartDate and [forms]![fromWhatDates]![txtEndDate]

    Then all you have to do on your buttonclick is check to see if the start and end date are entered and run the report if they are.

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help and welcome to the site!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 6
    Last Post: 06-28-2014, 07:45 PM
  2. Replies: 5
    Last Post: 05-07-2014, 09:45 AM
  3. Replies: 1
    Last Post: 10-21-2013, 09:42 AM
  4. List Dates between Start and Finish Dates
    By Buddus in forum Queries
    Replies: 1
    Last Post: 07-26-2013, 01:58 PM
  5. Replies: 3
    Last Post: 04-11-2012, 01:13 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