Results 1 to 6 of 6
  1. #1
    bakkouz is offline Novice
    Windows XP Access 2007
    Join Date
    Jan 2011
    Posts
    25

    Filter a report based on a couple of multiselect listboxes

    Hey guys,
    I have a report that is filtered based on a multiselect listbox on a form.
    which is working fine.
    what i want to do is add another multiselect listbox and filter the report based on both listboxes.
    which i don't really know how to do.

    Here is the code i'm using:

    Private Sub Command7_Click()
    Dim strReport As String
    Dim strWhere As String
    Dim lngView As Long
    Dim strFilter As String
    Dim varItem As Variant

    strReport = "Main_DB_Report" 'Put your report name in these quotes.
    lngView = acViewPreview 'Use acViewNormal to print instead of preview.

    ' loop through listbox items selected
    For Each varItem In Me!List0.ItemsSelected
    strFilter = strFilter & "[country] = '" & _
    Me!
    [List0].ItemData(varItem) & "' OR "


    Next ' continue loop


    If strFilter <> "" Then
    strFilter = Left(strFilter, Len(strFilter) - 4)
    Else
    MsgBox "You did not select any Country."
    List0.SetFocus
    Exit Sub
    End If


    DoCmd.OpenReport strReport, acViewPreview, , strFilter
    End Sub

    I've attached the sample database.productiondb.zip

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    What is the second listbox for?

    Maybe have another loop for the second listbox then concatenate the two strings with an AND operator.

    Make sure each string of OR criteria will be enclosed in parenthesis, like:

    (lb1a OR lb1b OR lb1c) AND (lb2a OR lb2b OR lb2c)
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    bakkouz is offline Novice
    Windows XP Access 2007
    Join Date
    Jan 2011
    Posts
    25
    June,
    The other listbox is for year.
    and i'm not really sure how to implement your instructions, i'm a beginner still, sorry.
    do i just replicate the above code for the loops and adjust it to the second list box?
    and where would i concatenate the two strings with an AND operator?

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Suggest two more variables:

    strFilter1
    strFilter2

    Build each string with looping structures.

    Concatenate:

    strFilter = "(" & Left(strFilter1, Len(strFilter1) - 4) & ") AND (" & Left(strFilter2, Len(strFilter2) - 4) & ")"
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    bakkouz is offline Novice
    Windows XP Access 2007
    Join Date
    Jan 2011
    Posts
    25
    June,
    I have done as you instructed.
    but now when clicking the button i am getting:
    error 2205 (default printer driver is not set up correctly)

    Here is the full code:
    Private Sub Command7_Click()
    Dim strReport As String
    Dim strWhere As String
    Dim lngView As Long
    Dim strFilter As String
    Dim strFilter3 As String
    Dim strFilter2 As String
    Dim varItem As Variant

    strReport = "Main_DB_Report" 'Put your report name in these quotes.
    lngView = acViewPreview 'Use acViewNormal to print instead of preview.

    ' loop through listbox items selected
    For Each varItem In Me!List0.ItemsSelected
    strFilter = strFilter & "[country] = '" & _
    Me!
    [List0].ItemData(varItem) & "' OR "
    Next ' continue loop
    '
    ' the next bit of code will subtract out the last "OR"
    If strFilter <> "" Then
    strFilter = Left(strFilter, Len(strFilter) - 4)
    Else
    MsgBox "You did not select any Country."
    List0.SetFocus
    Exit Sub
    End If

    ' loop through listbox items selected
    For Each varItem In Me!List5.ItemsSelected
    strFilter2 = strFilter2 & "[year] = '" & _
    Me!
    [List5].ItemData(varItem) & "' OR "
    Next ' continue loop
    '
    ' the next bit of code will subtract out the last "OR"
    If strFilter2 <> "" Then
    strFilter2 = Left(strFilter2, Len(strFilter2) - 4)
    Else
    MsgBox "You did not select any year."
    List5.SetFocus
    Exit Sub
    End If

    '
    ' now, run the report using strFilter to pass a string
    ' containing the needed customers
    strFilter3 = "(" & Left(strFilter, Len(strFilter) - 4) & ") AND (" & Left(strFilter2, Len(strFilter2) - 4) & ")"


    DoCmd.OpenReport strReport, acViewPreview, , strFilter3


    End Sub

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Don't know why that error would trigger.

    Have you step debugged? Follow the code as it executes. Does filter string get properly constructed? Refer to link at bottom of my post for debugging techniques.

    You probably want to use conditional code in the concatenation of the two filter strings, in case one or both are empty so don't end up with something like:

    ( ) AND (value1 OR value2)
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Multiselect to pull report from Access Form
    By Jackfam58 in forum Programming
    Replies: 3
    Last Post: 08-01-2012, 01:23 PM
  2. Couple of Questions
    By cade1980 in forum Access
    Replies: 2
    Last Post: 04-15-2012, 12:00 PM
  3. Replies: 3
    Last Post: 10-31-2011, 04:54 PM
  4. Couple of newbie questions
    By Awowk in forum Access
    Replies: 12
    Last Post: 08-06-2010, 01:16 PM
  5. A couple of problems...
    By dr_destructo in forum Forms
    Replies: 1
    Last Post: 06-08-2010, 11:03 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