473,420 Members | 1,579 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,420 software developers and data experts.

Applying ComboBox filters Sequentially

SueHopson
47 32bit
Hi All,

I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On the microsoft forum I found multiple examples of code from HansV and simliar code from NeoPa here that I am trying to understand and adapt.

The code below is works great for me so long as both independant combo boxes have selections, but despite everything I've read, it doesn't seem to leave all the values for the field when the one or either of the combo boxes is left blank.
Expand|Select|Wrap|Line Numbers
  1. Private Sub FilterMe()
  2.     Dim strWhere As String
  3.  
  4.     If Not IsNull(Me.cmbVendorSelect) Then
  5.         strWhere = strWhere & " AND [PartVendor]='" & Me.cmbVendorSelect & "'"
  6.         'how is this handled if the cbo is left blank? trying to learn...
  7.     End If
  8.  
  9.     If Not IsNull(Me.cmbTypeSelect) Then
  10.         strWhere = strWhere & " AND [PartType]='" & Me.cmbTypeSelect & "'"
  11.     End If
  12.  
  13.     If strWhere = "" Then
  14.         Me.Filter = ""
  15.         Me.FilterOn = False
  16.         'code seems to fail here when both comboboxes are blank, or am I    misunderstanding the code?
  17.     Else
  18.         Me.Filter = Mid(strWhere, 6)
  19.         Me.FilterOn = True
  20.     End If
  21. End Sub
Where I am having trouble is with the AND logic of the script, when a combo box is left blank (i.e they might just want to filter by Vendor or Part Type only). I have run the individual codes directly from the combo boxes using AFTER UPDATE, but am having the same trouble when I call the procedure.

As always, any help is greatly appreciated.
Dec 8 '23 #1

✓ answered by NeoPa

Hi Sue.

I have a couple of items out there that talk about filtering so I'm not sure which is the one you're referring to in your question, but the code looks like something I'd write &, as of this point in time when I'm just starting to look at it, I see no problem with it. Let me see if I can dig further.

No. I can't see why your code would fail. It looks fine to me. If you continue to find your system fails it would be helpful to see the results of executing the following code in your Immediate Pane :
Expand|Select|Wrap|Line Numbers
  1. ?Me.cmbVendorSelect;"|";Me.cmbTypeSelect;"|";strWhere;"|";Me.Filter;"|";Me.FilterOn;
This should be run immediately after noticing the attempt has failed. Please Copy & Paste the results of that from there into your next post.

Separately, you have two questions embedded within your code so I'll try to answer them here :
  1. 'how is this handled if the cbo is left blank? trying to learn...
    This is on line #6. If it is blank then line #4 ensures this code isn't run. An unbound ComboBox with no selection will return Null.
  2. 'code seems to fail here when both comboboxes are blank, or am I misunderstanding the code?
    I would say it's a fair guess you are misunderstanding the code, but I'm not sure how that would have any impact when it fails as that would be an act of observation.
    Certainly I see no problem with the code. It sets the Filter Property to an empty string and also tells the Form not to apply any filter. I would expect this is what you want when neither ComboBox has been selected from.

3 30183
NeoPa
32,556 Expert Mod 16PB
Hi Sue.

I have a couple of items out there that talk about filtering so I'm not sure which is the one you're referring to in your question, but the code looks like something I'd write &, as of this point in time when I'm just starting to look at it, I see no problem with it. Let me see if I can dig further.

No. I can't see why your code would fail. It looks fine to me. If you continue to find your system fails it would be helpful to see the results of executing the following code in your Immediate Pane :
Expand|Select|Wrap|Line Numbers
  1. ?Me.cmbVendorSelect;"|";Me.cmbTypeSelect;"|";strWhere;"|";Me.Filter;"|";Me.FilterOn;
This should be run immediately after noticing the attempt has failed. Please Copy & Paste the results of that from there into your next post.

Separately, you have two questions embedded within your code so I'll try to answer them here :
  1. 'how is this handled if the cbo is left blank? trying to learn...
    This is on line #6. If it is blank then line #4 ensures this code isn't run. An unbound ComboBox with no selection will return Null.
  2. 'code seems to fail here when both comboboxes are blank, or am I misunderstanding the code?
    I would say it's a fair guess you are misunderstanding the code, but I'm not sure how that would have any impact when it fails as that would be an act of observation.
    Certainly I see no problem with the code. It sets the Filter Property to an empty string and also tells the Form not to apply any filter. I would expect this is what you want when neither ComboBox has been selected from.
Dec 8 '23 #2
SueHopson
47 32bit
Ok, so.... I just moved the tables and form into a blank (test) database, setting the tables as local and reworked my button code from scratch. I'm fairly certain I can laugh at and blame myself for this one...

It's amazing what a change in logic it makes when you realize that = "" should actually be = Null in your reset code...

Expand|Select|Wrap|Line Numbers
  1. Private Sub btnResetFilters_Click()
    Me.Filter = ""
    Me.FilterOn = False
    Me.cmbTypeSelect = Null
    Me.cmbVendorSelect = Null
    End Sub
It now works fine. I then copied that form back into my main database, with linked tables, and it's now working properly too. The worst injuries to one's ego are always self-inflicted LOL
Dec 8 '23 #3
NeoPa
32,556 Expert Mod 16PB
Hi Sue.

We all manage to slip over things from time-to-time so you needn't be too hard on yourself for this.

Actually, this is a very common area for confusion. When is a value a ZLS (Zero-Length String, Empty string or "") and when is it a Null. Personally I'm very conscious of the need to determine what I'm working with nowadays, hence I can usually answer with some certainty. Others prefer the belt & braces (You may refer to them as suspenders.) approach where you do the tests in a way that handles both in the same way - especially for strings but for numbers too sometimes.

So, instead of using If Not IsNull(Me.cmbVendorSelect) Then, you could use either If Nz(Me.cmbVendorSelect, "") <> "" Then or even If Me.cmbVendorSelect > "" Then. Either of these replacements would yield the same result regardless of whether the item were a Null or a ZLS. Many choose to use such a format routinely and they are thus protected from ever having such issues bite them.

I prefer to be bitten early on and get the code to work as I require, but that doesn't mean you should. It may make sense to use such an approach & reap shorter development times in your work.
Dec 8 '23 #4

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

Similar topics

3
by: ssb | last post by:
Hello, This may be very elementary, but, need help because I am new to access programming. (1) Say, I have a column EMPLOYEE_NAME. How do I fetch (maybe, cursor ?) the values one by one and...
2
by: Marksmanaz | last post by:
I have a form named with a subform named based on a query with two controls called (text) and (Yes/No Checkbox). When the checkbox value is yes another query filters for Available Counselors...
19
by: Bernie Yaeger | last post by:
Everyone misses the point on this - what we need is a combobox autocomplete that is a dropdownlist only. When in dropdown mode, you can enter text - making that autocomplete is trivial. But when...
0
by: bcreighton | last post by:
I have created a bound subform on an unbound masterform linked together with a common field (A store's identification number) using an unbound combobox on the masterform and an invisible field on...
2
by: brino | last post by:
hi all ! my client wants to bring out data into a report with a number of filters that are chosen by the user . eg. -Section -Days to search -Type of care ive never tried this before...
1
by: The.Daryl.Lu | last post by:
Hi, two parts to my problem if someone can help address either one or both: 1. I want to SELECT everything in the table if it matches the criteria when the query button is pressed (this is just...
3
by: goscottie | last post by:
I have two comboboxes databound at run time. I can make a selection on first one to filter the second one. This works good with applying DataView RowFilter on second combobox. But at that...
0
by: CatchSandeepVaid | last post by:
We all know that one-to-one associations are non-lazly fetched but during this fetching the filters are not applied. I debugged hibernate code and found that hibernate finally calls...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.