Solved

Multi selection query

Posted on 2014-04-21
4
196 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 47

Accepted Solution

by:
Dale Fye (Access MVP) 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 47

Expert Comment

by:Dale Fye (Access MVP)
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

751 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