Solved

Multi selection query

Posted on 2014-04-21
4
185 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
Comment Utility
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
Comment Utility
That looks great I will try it and respond. Thank you.
0
 

Author Closing Comment

by:DatabaseDek
Comment Utility
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)
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Familiarize people with the process of utilizing SQL Server views 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 Access…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

771 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

13 Experts available now in Live!

Get 1:1 Help Now