Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    tarhim47 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Nov 2010
    Posts
    57

    HELP! using list boxes with multiselect

    Here is my file.

  2. #2
    weekend00 is offline I may not be right
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    1,295
    There is you file?

  3. #3
    tarhim47 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Nov 2010
    Posts
    57
    Hey,

    Sorry I posted the file onto a 2nd thread with my question on another. Didnt know how to update the original thread.

    In any case, I'm trying to create a form which includes a list box which allows the user to select multiple counties and have the form rertun information for all clients residing in the selected counties.

    The idea is to have the list box include all possible counties using a value list option.

    It works fine if i make it a text box and enter one county but i want to be able to select multiple counties.

    Any ideas?

    P.S. the data is all fake!

  4. #4
    weekend00 is offline I may not be right
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    1,295
    you must can find some good example from google for these cases.

  5. #5
    tarhim47 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Nov 2010
    Posts
    57
    I have found a bunch of examples which helped me to create the form and then link it to the report but i cant seem to find any examples where multiselect list boxes are used.

  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,521
    Quote Originally Posted by tarhim47 View Post
    i cant seem to find any examples where multiselect list boxes are used.
    That seems like a funny statement given that I gave you an example here:

    https://www.accessforums.net/forms/u...uery-9103.html
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    tarhim47 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Nov 2010
    Posts
    57
    I tried this...but what this does is provide all the counties in the table in the mutiselect list.

    For example:

    If my table has the following information
    1. client a - oxford
    2. client b - norwich
    3. client c - oxford
    4. client d - elgin
    5. client e - peel

    Then the multiselect list returns the following options
    1. oxford
    2. norwich
    3. oxford
    4. elgin
    5. peel

    Notice how the multiselect list is just a copy of the counties from the table above. This doesnt really help.

    What i want is for the multiselect list to return the following choices:
    - oxford
    - norwich
    - elgin
    - peel

    This way, if I choose oxford, the form will return two records instead of me having to go through and find all the oxfords and select them.

    When I'm done, im going to have about 100 records. If 50 of them are in oxford then it will make it diffficult to the use your example.

    Any other ideas?

  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,521
    Typically I would expect a "Counties" table that contained a list of available counties. If you want to generate the list from your data, rather than the table base the listbox on a query:

    SELECT Sample_data.County
    FROM Sample_data
    GROUP BY Sample_data.County
    ORDER BY Sample_data.County;

    That will provide a list of unique counties.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    tarhim47 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Nov 2010
    Posts
    57
    Hey Paul,

    The multiselect list worked...it now only shows all the different counties in alphabetical order.

    However it doesnt return any data.

    Can you please try this for me?

    Use the following info:
    - herd size - low: 1, high: 1000
    - age - low: 1, high: 1000
    - county: oxford and elgin

    It should return a few records in the report.

    What am I missing?

    Thanks for your patience!

  10. #10
    tarhim47 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Nov 2010
    Posts
    57
    Any thoughts?

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Am I allowed a lunch break?

    Did you add code to filter the report using the listbox as demonstrated in my sample? You have to use code, the criteria in the query won't work.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    tarhim47 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Nov 2010
    Posts
    57
    Hey Paul, hope you had a good week-end.

    And to answer your earlier question, NO (!) you are not allowed a lunch break....hahahaha...lmao! Jus kd, its just that I take a later lunch break which is why I didnt realize the time.

    In any case, I tried your code idea and inserted the code into the form and adjusted it to my database. However, it still doesnt return any data.

    Here is the code as I've inserted it:



    Option Compare Database
    Option Explicit

    Private Sub OK_Click()
    On Error GoTo Err_OK_Click

    Dim strWhere As String
    Dim ctl As Control
    Dim varItem As Variant
    'make sure a selection has been made
    If Me.Enter_county_of_farm_operations.ItemsSelected.C ount = 0 Then
    MsgBox "Must select at least 1 county"
    Exit Sub
    End If
    'add selected values to string
    Set ctl = Me.Enter_county_of_farm_operations
    For Each varItem In ctl.ItemsSelected
    'strWhere = strWhere & ctl.ItemData(varItem) & ","
    'Use this line if your value is text
    strWhere = strWhere & "'" & ctl.ItemData(varItem) & "',"
    Next varItem
    'trim trailing comma
    strWhere = Left(strWhere, Len(strWhere) - 1)
    'open the report, restricted to the selected items
    DoCmd.OpenReport "Report", acPreview, , "ClientID IN(" & strWhere & ")"
    Exit_OK_Click:
    Exit Sub
    Err_OK_Click:
    MsgBox Err.Description
    Resume Exit_OK_Click
    End Sub





    At this point, I am pretty much satisfied with my database. The only thing I need to finish is to be able to allow the user to select multiple counties and have the report return the data for just those counties.
    Last edited by tarhim47; 11-08-2010 at 10:28 AM. Reason: changed code

  13. #13
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Did you take the county criteria out of the query? This doesn't look correct:

    DoCmd.OpenReport "Report", acPreview, , "ClientID IN(" & strWhere & ")"
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  14. #14
    tarhim47 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Nov 2010
    Posts
    57
    I did...there is no criteria in the Query for County.

    As for the following line of code, I'm not exactly sure how to fix it. The ClientID is the primary key in the table.

    DoCmd.OpenReport "Report", acPreview, , "ClientID IN(" & strWhere & ")"

    Can you help me fix it?

    Also the error I'm getting when I click OK is as follows:

    "This expression is typed incorrectly, or it is too complex to be evaluated. For example, a numeric expression may contain too many complicated elements. Try simplifying the expression by assigning parts of the expression to variables."

  15. #15
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    But you want this restriction on County, so that should be the field specified.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. 2 multi select list boxes on one search form
    By woodey2002 in forum Forms
    Replies: 2
    Last Post: 11-05-2010, 12:44 PM
  2. Multi-select List Boxes
    By Rawb in forum Programming
    Replies: 6
    Last Post: 09-21-2010, 09:02 AM
  3. Search form with list boxes
    By scottay in forum Programming
    Replies: 15
    Last Post: 07-27-2010, 09:28 AM
  4. List Boxes.
    By Willtc in forum Programming
    Replies: 2
    Last Post: 02-12-2010, 04:12 AM
  5. List box to populate other list boxes
    By Nathan in forum Forms
    Replies: 0
    Last Post: 03-03-2009, 07:22 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