Results 1 to 8 of 8
  1. #1
    bjornas is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jan 2015
    Posts
    17

    Filter a report by means of a list box

    I am struggling to teach myself Access VBA. I do feel that the initial step is quite large, so I'd appreciate your help on this.

    I want to be able to create a filtered report, so that the user applies the filter by means of a list box. The user should be able to choose several values from the list box.

    My project is a quite large inventory database for my house. The inventory items is stored in (quite a number of) boxes. I want to create a filtered report so that only the contents of i.e. Box 1, Box 2 and Box 3 is in the list.

    I borrowed a code sample from http://www.fabalou.com/Access/Reports/filterreport.asp, and adapted it to fit my database. But I can't get it to work.

    Here is my code:
    Private Function GetCriteria() As String
    Dim stDocCriteria As String
    Dim VarItm As Variant
    For Each VarItm In ListFilter.ItemsSelected
    stDocCriteria = stDocCriteria & "[Box] = " & ListFilter.Column(0, VarItm) & " Or """
    Next

    If stDocCriteria <> "" Then
    stDocCriteria = Left(stDocCriteria, Len(stDocCriteria) - 4)
    Else
    stDocCriteria = "True"
    End If


    GetCriteria = stDocCriteria
    End Function


    And then there is a command button:
    Private Sub Command2_Click()

    DoCmd.OpenReport "Overview", acPreview, , GetCriteria()



    End Sub



    I expected it to produce a filtered report based in the user's input in the ListFilter listbox. However, a "Enter Parameter Box" shows up, requesting the user for an input for precisely the item chosen in the list box (if several items were chosen, then a syntax error pops up.

    Can somebody please help me on the right track?

    Thank You.

  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,518
    If Box is a text field you need delimiters around each value, similar to

    http://www.baldyweb.com/multiselect.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    bjornas is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jan 2015
    Posts
    17
    The box field is indeed a text field. I tried to implement tha cosw you referred me to, but in vain. Could I ask you, or anyone on this forum, to help me correct my code? I understand that programming is hard to get to, but this is really Greek to me.

  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,518
    Try

    stDocCriteria = stDocCriteria & "[Box] = '" & ListFilter.Column(0, VarItm) & "' Or "
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    bjornas is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jan 2015
    Posts
    17
    Sorry, that did not work. Perhaps I should point out that I use a special table with "allowable boxes" to populate the ListFom.This because in the main database, each record (with one "item") contains a "box" field.

    Does this screw something up?

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    I'm not following what you mean. Can you post the db, or a picture of the table?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    bjornas is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jan 2015
    Posts
    17
    This was so annoying I ended up with paying a dude for helping me with the code. As a thank you to this community, I post the code that he suggested:

    Private Sub Command2_Click()
    Dim stDocCriteria As String
    Dim VarItm As Variant
    For Each VarItm In ListFilter.ItemsSelected
    stDocCriteria = stDocCriteria & "[Plassering] = '" & ListFilter.Column(0, VarItm) & "' Or " '"
    Next

    If stDocCriteria <> "" Then
    stDocCriteria = Left(stDocCriteria, Len(stDocCriteria) - 4)
    Else
    stDocCriteria = "True"
    End If



    DoCmd.OpenReport "Oversikt", acPreview, , stDocCriteria


    End Sub

    ------------------------

    Not unlike your suggestions, but not going via a function. And I also messed up my own logics....

    Thank you for your help. This is a marvellous forum.

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Glad you got it sorted.
    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. Use MS Access to filter and report on a SharePoint list
    By Marcus Curry in forum SharePoint
    Replies: 2
    Last Post: 10-13-2014, 09:43 AM
  2. Replies: 7
    Last Post: 06-08-2014, 03:11 PM
  3. Replies: 15
    Last Post: 11-20-2013, 04:30 PM
  4. Can someone explain what this means?
    By Jay13 in forum Access
    Replies: 5
    Last Post: 05-23-2013, 04:22 PM
  5. Filter Report By list box
    By BorisGomel in forum Access
    Replies: 1
    Last Post: 04-21-2011, 03:26 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