473,378 Members | 1,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How do I filter a Listbox with multiple comboboxes?

I have a Form with four comboboxes (cboSelectDepartment, cboSelectOperation, cboSelectModel and cboSelectVariant), a Listbox (selectionList) and a Button (cmdSelectStep).

The Listbox displays a StepID (number) and a StepName (text).I want to filter the Listbox based upon the values in the comboboxes. After each combobox selection the List becomes shorter and shorter.

Next, the user selects a StepID number from the Listbox and presses the Button to set a Temporary Variable (StepRecord).

To make things complicated: The combobox cboSelectOperation is Cascaded from cboSelectDepartment and cboSelectVariant is Cascaded from cboSelectModel. So I the user selects a Department, the Operation combobox list is limited, the same with Model and Variants.

I've found a tutorial database ("Cascading Combo Boxes") where all the comboboxes are cascaded. Next, I've copied the code and adapted it to my database. But now I'm stuck with the SQL code for filtering (see Private Sub filterList()). This is the code I have now:


Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Private Sub filterList()
  5.     Dim strRS As String
  6.  
  7.   ' Filter the list box appropriately based on the combo box selection(s)
  8.   strRS = "SELECT qryFilterList.StepID, qryFilterList.StepName FROM qryFilterList"
  9.  
  10.   If Not IsNull(Me.cboSelectVariant) Then
  11.     strRS = strRS & " WHERE VariantID = " & Me.cboSelectVariant
  12.   ElseIf Not IsNull(Me.cboSelectModel) Then
  13.     strRS = strRS & " WHERE ModelID = " & Me.cboSelectModel
  14.   ElseIf Not IsNull(Me.cboSelectOperation) Then
  15.     strRS = strRS & " WHERE OperationID = " & Me.cboSelectOperation
  16.   ElseIf Not IsNull(Me.cboSelectDepartment) Then
  17.     strRS = strRS & " WHERE DeptID = " & Me.cboSelectDepartment
  18.   End If
  19.  
  20.   strRS = strRS & " ORDER BY qryFilterList.StepName;"
  21.   Me.selectionList.RowSource = strRS
  22.   Me.selectionList.Requery
  23. End Sub
  24.  
  25. Private Sub cboSelectDepartment_AfterUpdate()
  26.  
  27.   Me.cboSelectOperation.RowSource = "SELECT tblOperations.OperationID,tblOperations.Operation, tblOperations.Description FROM tblOperations " & _
  28.      " WHERE DeptID = " & Nz(Me.cboSelectDepartment) & _
  29.      " ORDER BY Operation"
  30.   Me.cboSelectOperation = Null
  31.   filterList
  32. End Sub
  33.  
  34. Private Sub cboSelectModel_AfterUpdate()
  35.  
  36.   Me.cboSelectVariant.RowSource = "SELECT qryVariant.variantID, qryVariant.Variant FROM qryVariant " & _
  37.      " WHERE ModelID = " & Nz(Me.cboSelectModel) & _
  38.      " ORDER BY Variant"
  39.   Me.cboSelectVariant = Null
  40.   filterList
  41. End Sub
  42.  
  43. Private Sub cboSelectOperation_AfterUpdate()
  44.   filterList
  45. End Sub
  46.  
  47. Private Sub cboSelectVariant_AfterUpdate()
  48.   filterList
  49. End Sub
  50.  
  51. Private Sub Form_Load()
  52.   filterList
  53.   Me.selectionList.RowSource = ""
  54. End Sub
  55.  
  56. Private Sub EnableControls()
  57.  
  58.   ' Clear the combo boxes
  59.   If IsNull(Me.cboSelectDepartment) Then
  60.      Me.cboSelectOperation = Null
  61.   End If
  62.  
  63.   If IsNull(Me.cboSelectOperation) Then
  64.     Me.cboSelectModel = Null
  65.   End If
  66.  
  67.   If IsNull(Me.cboSelectModel) Then
  68.     Me.cboSelectVariant = Null
  69.   End If
  70.  
  71.   ' Enable or disable combo boxes based on whether the combo box preceeding it has a value.
  72.   Me.cboSelectOperation.Enabled = (Not IsNull(Me.cboSelectDepartment))
  73.   Me.cboSelectModel.Enabled = (Not IsNull(Me.cboSelectOperation))
  74.   Me.cboVariant.Enabled = (Not IsNull(Me.cboSelectModel))
  75. End Sub
This code partially works! If I select from the Department and Operation comboboxes the List is filtered properly. But when I select a Model, the Department and Operation filter is removed/ignored.

I think I need to put a AND operator in the SQL part to combine all the filters. Unfortunately I don't have any experience with creating a SQL code. Can yo guys help me or point me in the right direction?

Thanks!
Nov 7 '14 #1

✓ answered by MikeTheBike

Hi

This is completly air code but should be somewhere near
Expand|Select|Wrap|Line Numbers
  1. Private Sub filterList()
  2.     Dim strRS As String
  3.  
  4.     ' Filter the list box appropriately based on the combo box selection(s)
  5.     If Me.cboSelectVariant.ListIndex >= 0 Then strRS = strRS & " AND VariantID = " & Me.cboSelectVariant
  6.  
  7.     If Me.cboSelectModel.ListIndex >= 0 Then strRS = strRS & " AND ModelID = " & Me.cboSelectModel
  8.  
  9.     If Me.cboSelectOperation.ListIndex >= 0 Then strRS = strRS & " AND OperationID = " & Me.cboSelectOperation
  10.  
  11.     If Me.cboSelectDepartment.ListIndex >= 0 Then strRS = strRS & " AND DeptID = " & Me.cboSelectDepartment
  12.  
  13.  
  14.     'IF FILTER FOUND ADD 'WHERE' AND REMOVE LEADING 'AND' (IF NOT FOUND IT WILL REMAIN A NULL STRING)
  15.     If strRS <> "" Then strRS = " WHERE " & Mid(strRS, 6)
  16.  
  17.     'ADD FILTER TO QUERY
  18.     strRS = "SELECT qryFilterList.StepID, qryFilterList.StepName FROM qryFilterList " & strRS
  19.  
  20.     'ADD ORDER BY CLAUSE
  21.     strRS = strRS & " ORDER BY qryFilterList.StepName;"
  22.  
  23.  
  24.     Me.selectionList.RowSource = strRS
  25.     Me.selectionList.Requery
  26. End Sub
I have used the 'ListIndex' property, which is -1 if nothing is selected, instead of Not IsNull()

It also assumes all criteria are numeric.

HTH


MTB

2 5893
MikeTheBike
639 Expert 512MB
Hi

This is completly air code but should be somewhere near
Expand|Select|Wrap|Line Numbers
  1. Private Sub filterList()
  2.     Dim strRS As String
  3.  
  4.     ' Filter the list box appropriately based on the combo box selection(s)
  5.     If Me.cboSelectVariant.ListIndex >= 0 Then strRS = strRS & " AND VariantID = " & Me.cboSelectVariant
  6.  
  7.     If Me.cboSelectModel.ListIndex >= 0 Then strRS = strRS & " AND ModelID = " & Me.cboSelectModel
  8.  
  9.     If Me.cboSelectOperation.ListIndex >= 0 Then strRS = strRS & " AND OperationID = " & Me.cboSelectOperation
  10.  
  11.     If Me.cboSelectDepartment.ListIndex >= 0 Then strRS = strRS & " AND DeptID = " & Me.cboSelectDepartment
  12.  
  13.  
  14.     'IF FILTER FOUND ADD 'WHERE' AND REMOVE LEADING 'AND' (IF NOT FOUND IT WILL REMAIN A NULL STRING)
  15.     If strRS <> "" Then strRS = " WHERE " & Mid(strRS, 6)
  16.  
  17.     'ADD FILTER TO QUERY
  18.     strRS = "SELECT qryFilterList.StepID, qryFilterList.StepName FROM qryFilterList " & strRS
  19.  
  20.     'ADD ORDER BY CLAUSE
  21.     strRS = strRS & " ORDER BY qryFilterList.StepName;"
  22.  
  23.  
  24.     Me.selectionList.RowSource = strRS
  25.     Me.selectionList.Requery
  26. End Sub
I have used the 'ListIndex' property, which is -1 if nothing is selected, instead of Not IsNull()

It also assumes all criteria are numeric.

HTH


MTB
Nov 7 '14 #2
Hey Mike,

The code works Flawless! It's exactly what I wanted!

Thank you.
Nov 7 '14 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Alex Fimine | last post by:
Hi, I have a list box lstMy which is bound to a datatable dt. The SelectionMode in the listbox is set to MultiSimple. I want to run through the listbox and return all _values_ from the...
3
by: RRT | last post by:
I have an existing table which describes Streets and sections of streets between intersections: Table 1: Streets by Intersections Street Area Ann St. Main to Jackson...
4
by: Lucas Tam | last post by:
Hi all, Does anyone have an example on how to populate a Listbox with Text, a ComboBox Control, and a Checkbox control? Basically I want my listbox to look like this: Some Text ...
0
by: marcelo | last post by:
Could you help me out here. I need to accomplish this: I need to filter listbox items from by entering some characters to the textbox. But the problem is that items (files that are read from...
2
by: Matt | last post by:
Hi all, me again! :) I've now got an issue with combo boxes. Basically, I have a number of items that I want a user to pick from a single list. It's basically along the lines of: Fruit 1: ...
3
by: Randy | last post by:
I have a routine that creates a series of comboboxes, each of which is bound to a common dataview. Everything used to work fine, but now, when I change the value of any of the comboboxes, the...
1
by: Redbeard | last post by:
Hi, I am a newbie using Access 2003 and I am trying to select multiple values from a list box and put them in a text box on the same Form. The closest thing in the form pages that I can find is some...
0
by: Gunnar Hurtig | last post by:
Hi All I am relatively new to Tkinter and am putting a wraparound to the ATNF ASAP program. In one part I present several long lists in list boxes for selection. My code will remember the multiple...
3
by: mbedford | last post by:
I'm creating an asset/employee/everything including the kitchen sink database for my office. Currently I'm working on formEmployee. It contains a listbox which contains all the employees, and...
1
by: rafeenatnat | last post by:
Hi, I can only bind 1 item even if I selected more than 1 item from my ListBox. Every time I select another item, my uniformGrid(grid to display selected items) will only display the first selection...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.