Solved

Multi selection query

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
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…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

809 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