Solved

Multi selection query

Posted on 2014-04-21
4
190 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
  • 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

912 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now