Results 1 to 7 of 7
  1. #1
    ice051505 is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Feb 2013
    Posts
    76

    Listbox MultiSelection

    Hi all, I have a piece of code for make the listbox multiple selection and it keep give me errors, can anyone take a look at my code and fix for me?



    Code:
    Private Sub Slc_Sub_AfterUpdate()
    
    Dim db As DAO.Database
    Dim qdf As DAO.QueryDef
    Dim rs As DAO.Recordset
    Dim strWhere As String
    Dim ctl As Control
    Dim varItem As Variant
    
    Set db = CurrentDb()
    Set qdf = db.QueryDefs("(Result) Selection")
    Set rs = db.OpenRecordset("Select * from [SubArea]", dbOpenSnapshot)
    
    If Me.Slc_Sub.ItemsSelected.Count = 0 Then
      MsgBox "Must select at least 1 subArea"
      Exit Sub
    End If
    
    Set ctl = Me.Slc_Sub
    For Each varItem In ctl.ItemsSelected
     strWhere = strWhere & "'" & ctl.ItemData(varItem) & "',"
    Next varItem
    
    strWhere = Left(strWhere, Len(strWhere) - 1)
    strSql = "SELECT " & rs.Fields(1).Value & " FROM [SubArea] WHERE (" & rs.Fields(1).Value & " IN (" & strWhere & "));"
    qdf.SQL = strSql
    
    End Sub
    The [SubArea] is a query contain only one column of information which selected from a option group, so it is changed depending on the different choice button clicked.
    The [(Result) Selection] is a query that I want to pass the multiple selection value from the Listbox to this query
    Slc_Sub is my ListBox

  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,870

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    What's the error and where is it?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    ice051505 is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Feb 2013
    Posts
    76
    Thanks for the reply!
    The error is at the strSql: Run-time error 3625: the item not found in this collection
    strSql = "SELECT " & rs.Fields(1).Value & " FROM [SubArea_Selection] WHERE (" & rs.Fields(1).Value & " IN (" & strWhere & "));"

    I know there maybe a syntex error, but I just can't fix it, can I get little help here?

    Quote Originally Posted by pbaldy View Post
    What's the error and where is it?

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    If that recordset only returns 1 field, it would be:

    rs.Fields(0)

    That's returning a field name?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    ice051505 is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Feb 2013
    Posts
    76
    Yes!!! you are so right! I got it!
    Thank you, pbaldy!!!

    Quote Originally Posted by pbaldy View Post
    If that recordset only returns 1 field, it would be:

    rs.Fields(0)

    That's returning a field name?

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Excellent! Happy to help.
    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. Replies: 3
    Last Post: 12-13-2012, 04:40 AM
  2. Replies: 1
    Last Post: 09-10-2012, 11:21 PM
  3. Replies: 1
    Last Post: 07-26-2012, 11:45 AM
  4. Replies: 7
    Last Post: 06-05-2012, 03:22 PM
  5. Replies: 3
    Last Post: 08-03-2010, 03:35 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