Solved

Multi selection query

Posted on 2014-04-21
4
197 Views
Last Modified: 2014-04-28
Imagine I have a continuous form that shows all of the cars I have sold in rows one per car. I have sold cars in 40 different colors. On the form how do I create the condition for the query that would show all cars for a selected range of colors but not cars with different colors.

If I wanted to see only cars sold in Red, Blue and Black how do I best select the colors in the first place and what is the best way to create the query condition under the field [Car Color] with the variables, "Red" Or "Blue" Or "Black"
0
Comment
Question by:DatabaseDek
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 48

Accepted Solution

by:
Dale Fye earned 500 total points
ID: 40013417
I would normally use a multi-select listbox (or possibly subform formatted to look like a listbox) for this functionality.  Display all of the car colors in the listbox, then you build a where clause based on the itemsSelected collection.  I actually have a function I use for this, using a syntax similar to:

strCriteria = "[Color] " & fnMultiSelect(me.lst_CarColors)

and the function looks like the following.  
If no items are selected in the list, the code returns: "IS NOT NULL"
IF one item is selected, it would return: "= 'Red'"
If multiple items are selected it would return: " IN('Red', 'Blue', 'Green')"
Public Function fnMultiList(lst As ListBox, Optional SelectAll As Boolean = False, _
                            Optional UseColumn As Integer = -1, _
                            Optional Delimiter As Variant = Null) As Variant

    Dim varItem As Variant
    Dim lngItem As Long
    Dim strDelimiter As String
    
    fnMultiList = Null
    
    'Determine how to delimit the list items
    If UseColumn = -1 Then UseColumn = lst.BoundColumn - 1
    If IsNull(Delimiter) = False Then
        strDelimiter = Delimiter
    ElseIf IsNumeric(lst.Column(UseColumn, Abs(lst.ColumnHeads))) Then
        strDelimiter = ""
    Else
        strDelimiter = Chr$(34)
    End If
    
    'loop through the selected items in the list
    If lst.MultiSelect = 0 And SelectAll = False Then
        fnMultiList = lst.Value
    Else
        For lngItem = 0 To lst.ListCount
            If lst.Selected(lngItem) = True Or SelectAll Then
                fnMultiList = (fnMultiList + ",") _
                            & strDelimiter & lst.Column(UseColumn, lngItem) _
                            & strDelimiter
            End If
        Next lngItem
    End If
    
    'Strip trailing "," if there is one
    If Right(fnMultiList, 1) = "," Then fnMultiList = Left(fnMultiList, Len(fnMultiList) - 1)
    
    'Depending on the number of items selected, determine how the "list values" are returned
    If Len(fnMultiList & "") = 0 Then
        fnMultiList = " IS NOT NULL"
    Else
        Select Case Len(fnMultiList) - Len(Replace(fnMultiList, ",", ""))
            Case 0
                fnMultiList = " = " & fnMultiList
            Case Else
                fnMultiList = " IN (" & fnMultiList & ")"
        End Select
    End If
    
End Function

Open in new window

0
 

Author Comment

by:DatabaseDek
ID: 40014234
That looks great I will try it and respond. Thank you.
0
 

Author Closing Comment

by:DatabaseDek
ID: 40027848
I am really sorry but due to other commitments I have not had time to try this solution. I cannot hold this up for any longer so I am allocating points now. Do you mind helping if I get stuck later?

with thanks derek
0
 
LVL 48

Expert Comment

by:Dale Fye
ID: 40027871
Yes, I'll be happy to follow-up with you when you get the time to address this.

For future reference, when EE starts bugging you about abandoned questions, if you simply post a comment that you have not had time to pursue it, they will not normally bug you for another couple of days, and you can usually get away with doing that several times before they lock you out.

If, on the other hand, you have too many open questions, I think they will make you close one or more of them out before asking more questions.
0

Featured Post

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question