Results 1 to 8 of 8
  1. #1
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776

    Report form with filter

    Hi all,
    I created this report form so i can select reports and open them... I added a filter lstbox from allen brown and I am wondering if it can be altered so that
    it would be able to filter from my lstbox selection of reports instead of just one. Currently I have apply filter button which works, but only will open one report
    The goal is to be able to filter all the reports based on selection of my LEFT listbox, See below.



    Code:
    Private Sub Ctl_CmdPreview_Click()
    On Error GoTo Err_Handler
        'Purpose:  Open the report filtered to the items selected in the list box.
        'Author:   Allen J Browne, 2004.   http://allenbrowne.com
        Dim varItem As Variant      'Selected items
        Dim strWhere As String      'String to use as WhereCondition
        Dim strDescrip As String    'Description of WhereCondition
        Dim lngLen As Long          'Length of string
        Dim strDelim As String      'Delimiter for this field type.
        Dim strDoc As String        'Name of report to open.
        
        'strDelim = """"            'Delimiter appropriate to field type. See note 1.
        strDoc = "BusinessByIndustry"
    
    
        'Loop through the ItemsSelected in the list box.
        With Me.LstIndustryType
            For Each varItem In .ItemsSelected
                If Not IsNull(varItem) Then
                    'Build up the filter from the bound column (hidden).
                    strWhere = strWhere & strDelim & .ItemData(varItem) & strDelim & ","
                    'Build up the description from the text in the visible column. See note 2.
                    strDescrip = strDescrip & """" & .Column(1, varItem) & """, "
                End If
            Next
        End With
        
        'Remove trailing comma. Add field name, IN operator, and brackets.
        lngLen = Len(strWhere) - 1
        If lngLen > 0 Then
            strWhere = "[BusinessHelperID] IN (" & Left$(strWhere, lngLen) & ")"
            lngLen = Len(strDescrip) - 2
            If lngLen > 0 Then
                strDescrip = "IndustryType: " & Left$(strDescrip, lngLen)
            End If
        End If
        
        'Report will not filter if open, so close it. For Access 97, see note 3.
        If CurrentProject.AllReports(strDoc).IsLoaded Then
            DoCmd.Close acReport, strDoc
        End If
        
        'Omit the last argument for Access 2000 and earlier. See note 4.
        DoCmd.OpenReport strDoc, acViewPreview, WhereCondition:=strWhere, OpenArgs:=strDescrip
    
    
    Exit_Handler:
        Exit Sub
    
    
    Err_Handler:
        If Err.Number <> 2501 Then  'Ignore "Report cancelled" error.
            MsgBox "Error " & Err.Number & " - " & Err.Description, , "cmdPreview_Click"
        End If
        Resume Exit_Handler
    End Sub
    Click image for larger version. 

Name:	Reports.jpg 
Views:	10 
Size:	38.5 KB 
ID:	47136

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    Well does the data on the right have a field which identifies which category on the left it belongs to?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Do you want to open more than one report at the time or you just want to select the one report to open in the left listbox? Either way you need to make the strDoc take its value from the listbox instead of being hard-coded like you have it now:
    Code:
     With Me.LstReports
            For Each varItem In .ItemsSelected
               strDoc=.Column(1, varItem) 'assumes list box has two columns, the first one holding the reportID is the bound one and hidden    
    	   'now open the report
    	   DoCmd.OpenReport strDoc, acViewPreview, WhereCondition:=strWhere, OpenArgs:=strDescrip
    	Next
     End With
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    Quote Originally Posted by Gicu View Post
    Do you want to open more than one report at the time or you just want to select the one report to open in the left listbox? Either way you need to make the strDoc take its value from the listbox instead of being hard-coded like you have it now:
    Code:
     With Me.LstReports
            For Each varItem In .ItemsSelected
               strDoc=.Column(1, varItem) 'assumes list box has two columns, the first one holding the reportID is the bound one and hidden    
           'now open the report
           DoCmd.OpenReport strDoc, acViewPreview, WhereCondition:=strWhere, OpenArgs:=strDescrip
        Next
     End With
    Cheers,
    I read it as selecting a category from the left listbox, which would limit the entries in the right listbox?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Yes, thanks Welshgasman! It does

  6. #6
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Thanks Again Gicu,
    I just want one at a time open, just one button to open them as needed and to be able to filter if desired.
    Thanks

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    Quote Originally Posted by d9pierce1 View Post
    Yes, thanks Welshgasman! It does
    So in the afterupdate event of the left listbox, amend the source for the right listbox?
    However you would need a field in that source to know what to filter on?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  8. #8
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Worked like a charm! Thank you all so much
    that just makes my day
    Dave

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. Replies: 15
    Last Post: 07-14-2014, 11:04 AM
  3. Re-Filter report within form
    By bikeordie1 in forum Access
    Replies: 2
    Last Post: 04-02-2013, 08:14 AM
  4. Replies: 2
    Last Post: 04-05-2012, 12:22 PM
  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