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

How do I apply a form filter to a combo box?

Seth Schrock
2,965 Expert 2GB
I have filtered a form as part of my record access control. On this form, I have a combo box that I use to navigate to different records on the form. I need this combo box to have the same filter as the form. I could just set the record source in the form's On_Load event using the WHERE clause of the query, but I would rather just set it so that the recordsource of the combo box would be the same as the form so that I wouldn't have to mess with the combo box. Is this possible?
Jan 17 '13 #1

✓ answered by ADezii

It is not the usual approach, but I feel as though it can be done.
  1. If Active, the Form's Filter typically exists as:
    Expand|Select|Wrap|Line Numbers
    1. ((tblEmployees.Last="ADezii"))
  2. You would then need to:
    1. Replace '(' and ')' with Zero Length Strings.
    2. Replace '"' with '.
    3. This will construct the WHERE Clause which you can then Append to a Base String representing the Row Source of the Combo Box.
  3. The simplest scenario would then be:
    Expand|Select|Wrap|Line Numbers
    1. Dim strCriteria As String
    2.  
    3. If Me.FilterOn Then     'Is the Filter Active?
    4.   'Construct WHERE Clause for the Row Source
    5.   strCriteria = Replace(Replace(Replace(Me.Filter, "(", ""), ")", ""), """", "'")
    6.     Me![cboFilter].RowSource = "SELECT [Last] FROM tblEmployees WHERE " & strCriteria
    7. End If
  4. P.S. - This simple scenario does actually work.

5 1793
ADezii
8,834 Expert 8TB
It is not the usual approach, but I feel as though it can be done.
  1. If Active, the Form's Filter typically exists as:
    Expand|Select|Wrap|Line Numbers
    1. ((tblEmployees.Last="ADezii"))
  2. You would then need to:
    1. Replace '(' and ')' with Zero Length Strings.
    2. Replace '"' with '.
    3. This will construct the WHERE Clause which you can then Append to a Base String representing the Row Source of the Combo Box.
  3. The simplest scenario would then be:
    Expand|Select|Wrap|Line Numbers
    1. Dim strCriteria As String
    2.  
    3. If Me.FilterOn Then     'Is the Filter Active?
    4.   'Construct WHERE Clause for the Row Source
    5.   strCriteria = Replace(Replace(Replace(Me.Filter, "(", ""), ")", ""), """", "'")
    6.     Me![cboFilter].RowSource = "SELECT [Last] FROM tblEmployees WHERE " & strCriteria
    7. End If
  4. P.S. - This simple scenario does actually work.
Jan 17 '13 #2
Seth Schrock
2,965 Expert 2GB
Okay, I have slightly modified your code to fit my row source. Here it is:
Expand|Select|Wrap|Line Numbers
  1. Dim strCriteria As String
  2.  
  3. If Me.FilterOn Then     'Is the Filter Active?
  4.   'Construct WHERE Clause for the Row Source
  5.   strCriteria = Replace(Replace(Replace(Me.Filter, "(", ""), ")", ""), """", "'")
  6.     Me![cboRecordSearch].RowSource = "SELECT LoanID, Borrower & (', ' & LoanNumber) AS Combination " & _
  7.                                      "FROM tblLoans WHERE " & strCriteria & " ORDER BY Borrower"
  8. End If
My question now is do I need to create an Else in the If/Then statement to set the row source to the same query minus the WHERE clause? Some users don't have filters on the form (administrator, etc.) so the filter will be off.
Jan 17 '13 #3
ADezii
8,834 Expert 8TB
It is a simple matter to truncate the WHERE Clause if needed and apply it to the Form.
Jan 17 '13 #4
Seth Schrock
2,965 Expert 2GB
Okay. I got. Thanks ADezii!
Jan 17 '13 #5
NeoPa
32,556 Expert Mod 16PB
I would humbly (except I never do humble!) suggest that the filter be transferred exactly as is. We all appreciate that Access is somewhat clumsy when producing SQL (Access QueryDefs Mis-save Subquery SQL), but in most cases it does work.

On the other hand, removing parentheses from a WHERE clause can be highly dangerous, as you may throw away some important ones. Likewise with the quotes. Changing the quotes (and I truly sympathise as Access uses the wrong characters by default - See Quotes (') and Double-Quotes (") - Where and When to use them) is not a safe thing to do, and is unnecessary. Consider a situation where Access has Martin O'Brien in the filter as "Martin O'Brien". Change the quotes to single (SQL standard) and you end up with 'Martin O'Brien', which will cause a failure. Likewise with sixteen inches written as "16""". Converted this would be '16''' (and without the tags - '16'''). It may look the same when displayed, but is a completely different string.

Access's version is messy, but I suggest you leave it as-is nevertheless.
Jan 17 '13 #6

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

Similar topics

3
by: Mark | last post by:
I have a form that contains the following: 2 radio buttons (Name and Number) 1 check box (for Inactive Members) 1 combo box (list of Member names or numbers) I use the radio buttons to...
1
by: John | last post by:
I have a combo box in a form for ContactName and a combo box with CompanyName. I would like to limit the ConactName combo box to just the names associated with CompanyName. Could someone please...
3
by: Tony | last post by:
Hi, I wonder why when I do a filter by form, some text fields show all the data in the table in the combo box and others only show two items, Is Null and Not Is Null. I want to show all data,...
2
by: Schultzy | last post by:
I think Kazza has the same problem. I am very new at this I also need to get the values from the combo selection and put them in a table I've tried to put the line in the criteria but I don't...
3
by: imtmub | last post by:
Hi, I need to filter from one combo box to another in MsExcel. I have two combo box. one is Categary and another is product. If the user select categary depend on that second combo box filter only...
2
by: banderson | last post by:
Hello, I have a data entry form for a table with information about buildings and am having a problem making a combo box do what I want. I would like to make the combo box show a list of unique bldg...
6
by: banderson | last post by:
Hello, I have a form and a combo box that I use to filter the form to particular records. I also have an "add new" button that, when clicked, adds a new record. My problem is that the filter combo...
1
by: karinos57 | last post by:
Hi, I have this form called LOB Selection Tab. I want the user to go to this form and choose from the drop down box any selection he/she wants and then select one of the group options below like:...
18
by: Richard Penfold | last post by:
I have a form with a subform in my database that I use to manage deliveries. A combo box on the subform displays items of inventory and thier current status. The code behind the combo box is......
0
by: Elaine Huseby | last post by:
I have a form "PartsAndInvoices that has all the parts in the database listed in split form view. I want the user to use Access 2007's filter section on the ribbon to choose mutiple filters....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.