Results 1 to 4 of 4
  1. #1
    templeowls is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2019
    Posts
    305

    Need help with two questions about a form to filter report

    I have the below code in a form to filter a report. Date fields to allow the user to filter to a certain date set and location field to allow them to filter to a particular.



    I want to add code so that 1) if the user presses 'enter' on their keyboard after inputting a filter, it opens the report and then 2) if the user has inputted no filters but hits the button or presses enter, then the form does not open the report.

    I've tried looking for some guides online for both questions and had no luck. Any advice?

    Code:
    Private Sub btnOpenReport_Click()On Error GoTo Err_Handler
        Dim strReport As String
        Dim strDateField As String
        Dim strWhere As String
        Dim lngView As Long
        Dim strFilter As String
        Set frm = Forms!frmSearchTool
        strFilter = ""
        Const strcJetDate = "\#mm\/dd\/yyyy\#"
        
        strReport = "rptSearchReport"
        strDateField = "[SubmissionDate]"
        lngView = acViewReport
        
        'Date Filter
        If IsDate(Me.txtStartDate) Then
            strWhere = "(" & strDateField & " >= " & Format(DateValue(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
        
        'Location
        If Not IsNull(frm!ComboLocation) Then
            If strWhere <> "" Then
             strWhere = strWhere & " AND "
            End If
            strWhere = strWhere & "[Location] ='" & frm!ComboLocation & "'"
        End If
    
    
        If CurrentProject.AllReports(strReport).IsLoaded Then
            DoCmd.Close acReport, strReport
        End If
        
        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
    End Sub

  2. #2
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    For the first one try to assign the Default property of the btnOpenReport button to True. As for the second you simply add a check to see if any of the three controls are populated and skip the OpenReport Line if none are filled in (added just before Docmd.OpenReport line):
    Code:
    If IsNull(Me.txtStartDate) AND IsNull(Me.txtEndDate) AND IsNull(Me.ComboLocation) Then Exit Sub
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  3. #3
    templeowls is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2019
    Posts
    305
    Both worked!! Thanks so much!!

  4. #4
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114

    Happy New Year!
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

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

Similar Threads

  1. Replies: 7
    Last Post: 12-30-2021, 02:29 PM
  2. Report Questions
    By JustMike in forum Access
    Replies: 3
    Last Post: 01-25-2019, 02:29 PM
  3. Report questions
    By trangnguyen in forum Reports
    Replies: 4
    Last Post: 02-16-2018, 05:46 AM
  4. Re-Filter report within form
    By bikeordie1 in forum Access
    Replies: 2
    Last Post: 04-02-2013, 08:14 AM
  5. filter by form for report
    By stephenaa5 in forum Reports
    Replies: 1
    Last Post: 05-08-2010, 03:14 AM

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