Results 1 to 6 of 6
  1. #1
    yellowgelb is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Feb 2015
    Posts
    14

    Subform does not show results from a multi-select listbox

    Hi,
    I have created a unbounded multi-select list box in the form header called lstSpecies. In the detail of the form I would like to have a sub-form as a datasheet with different columns.
    I used the following code to select the names from the list box. The code works fine but it doesn't show me the selected names in the datasheet as a sub form. When I use the only fields from the fieldlist in the detail to show me the results the code works fine. So I added the red marked lines (see below), which I used for other parts of the same form, to see the result also in a datasheet subform. But it doesn't work. I don't get an error. Regardless of which name I choose from my lstSpecies list I always get the same 16 rows as a result, which not necessarily contain the chosen name. I don't understand that.

    I hope very much someone can help me to sort that out. Thanks a lot!! Anje

    [Private Sub cmdFilter_Click1()]
    [Dim strWhere As String]
    [Dim varItem As Variant]
    [Dim lngLen As Long]


    [With Me.lstSpecies]
    [For Each varItem In .ItemsSelected]
    [If Not IsNull(varItem) Then]
    [strWhere = strWhere & """" & .ItemData(varItem) & """, "]
    [End If]
    [Next varItem]
    [End With]

    [lngLen = Len(strWhere) - 2 'Without trailing comma and space.]


    [If lngLen > 0 Then]
    [strWhere = "[CatSpeSpeciesRef] IN (" & Left$(strWhere, lngLen) & ")"]
    [End If]

    [With Me!sfmBasicQuery.Form]
    [Me.Filter = strWhere]
    [Me.FilterOn = True]


    [End With]


    [End Sub]

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    You do NOT need all the square brackets "[" "]".
    You use [ and ] around variable and field names containing special characters and/or embedded spaces

    Try putting

    Debug.Print strWhere
    in this
    Code:
     If lngLen > 0 Then 
      strWhere = "[CatSpeSpeciesRef] IN (" & Left$(strWhere, lngLen) & ")" 
      Debug.Print  strWhere
      End If
    It should show you the value of strWhere in the immediate window

  3. #3
    yellowgelb is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Feb 2015
    Posts
    14
    Thanks a lot for your answer!
    Ignore the square brackets []. I don't have them around my code within Access. I have used them only here in the forum, because I thought I have to mark my code like that. (...."Use code tags to enclose your code: [ code]Your code here...[/code]."...) Now I know how I have to do that. Sorry.I used the Debug.Print command. That is an example for an expression I get in the immediate window: [CatSpeSpeciesRef] IN ("Australian magpie", "Australian pratincole")

    The code filters the correct results, when I use just simple fields as an output (without a subform). But do you have an idea, what I have to change within the code to get the same correct results also in a subform (datasheet). I added the red marked rows, which I also use for other parts of the form. But apparently that is not working with this code for multi-select list boxes.

    Thanks a lot for your help!

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I've been looking at your code and thinking about it. My first thought is that the code is executed from a button click event. But I have never seen the click event have a "1" at the end...
    Code:
    Private Sub cmdFilter_Click1()
    But you say the code works.



    So looking at the code this morning, it occured to me you are not setting the filter for the subform, you are setting the filter for the main form.
    Code:
        With Me!sfmBasicQuery.Form   '<= this should refer to the sub form
            Me.Filter = strWhere      '<= this (Me) refers to the main form
            Me.FilterOn = True        '<= this (Me) refers to the main form
        End With

    You might try:
    Code:
        With Me!sfmBasicQuery.Form
            .Filter = strWhere
            .FilterOn = True
        End With
    or
    Code:
        With Me!sfmBasicQuery.Form
            !Filter = strWhere
            !FilterOn = True
        End With
    I don't use subforms like this to remember the correct syntax.

  5. #5
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    The debug.print Yourvariable
    is a common debugging/checking technique to see What is actually rendered to be used by Access

    You will see incorrect syntax, or if you take a complete SQL query and debug.print it
    You can copy the rendered sql from the immediate window and put it in the query wizard and
    execute it. If it works good, you know the code is good. If you get a syntax error, you know what to fix (usually).
    Last edited by orange; 02-13-2015 at 07:20 PM. Reason: spelling

  6. #6
    yellowgelb is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Feb 2015
    Posts
    14
    Thanks a lot for your help! It is working without the Me and just an dot. Great. There is still a lot to learn. Also thanks to orange for your quick reply and the advice.

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

Similar Threads

  1. Multi-Select Listbox
    By RayMilhon in forum Forms
    Replies: 5
    Last Post: 03-04-2014, 11:54 AM
  2. multi select listbox
    By crowegreg in forum Forms
    Replies: 3
    Last Post: 07-28-2012, 01:48 PM
  3. Looping through a multi select listbox - how do I do it?
    By shabbaranks in forum Programming
    Replies: 4
    Last Post: 04-02-2012, 11:56 AM
  4. Need Multi-Select listbox code example
    By Buakaw in forum Forms
    Replies: 4
    Last Post: 08-21-2011, 08:37 PM
  5. Replies: 3
    Last Post: 06-22-2011, 08:51 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