Hi folks! I'm hoping someone can help me with this one (either with a solution or telling me it's impossible so I stop wasting time trying).
I have a subform that filters based on a text box on the main form. This works fine so long as the fields match. I, however, need to find results when the subform field is an exact match to part of the search field. For example:
The search text might be "ONE" and will correctly find all records where the applicable subform field is "ONE".
The problem is when the search contains multiple elements. For instance, a search for "ONE, TWO" needs to return all records where the applicable subform field is "ONE" OR "TWO", not just an exact match to "ONE, TWO".
I know how to do this in reverse, but can't figure out the way to set it up how I need. I have tried setting a filter in the Record Source of the subform and applying it using an after update VBA event, both with no joy.
Below is a snippet of a few code variations I have tried to use to apply the filter in the after update event. Thanks in advance for any help. -
Forms.fmTrainingReport.fmMemberItemsReport.Form.Filter = "[fldFullName] Like '" & Me.txtMemberFilter & "'"
-
-
Forms.fmTrainingReport.fmMemberItemsReport.Form.Filter = "*[fldFullName]* Like '" & Me.txtMemberFilter & "'"
-
-
Forms.fmTrainingReport.fmMemberItemsReport.Form.Filter = "'*' & [fldFullName] & '*' Like '" & Me.txtMemberFilter & "'"
-
You are correct that you won't be able to directly reference the textbox in the filter string. However, if the textbox has the different search string separated by a unique character (such as a comma) that won't ever be part of the search text, then you can use the Split() function to separate the different search strings into an array. Then add the quotes around the different values, and then use the Join() function to put all the parts back together with a separating comma and then you have your search string for the IN statement.
7 1728
If you are looking for an exact match of "ONE" or "TWO", then use the In statement instead of the = or Like statements.Field_Name IN ("ONE", "TWO")
Thanks for the help. I'm not sure, however, that this will work for me since my result is a single string (e.g. "ONE, TWO, THREE" as opposed to multiple items (e.g. "ONE","TWO","THREE"). Am I missing something?
You are correct that you won't be able to directly reference the textbox in the filter string. However, if the textbox has the different search string separated by a unique character (such as a comma) that won't ever be part of the search text, then you can use the Split() function to separate the different search strings into an array. Then add the quotes around the different values, and then use the Join() function to put all the parts back together with a separating comma and then you have your search string for the IN statement.
Thanks for the direction. I wasn't considering an array, but that should work.
The other option would be to use the Replace() function to replace the delimiting character with "," and then put a double quote at the beginning and ending of the string. - Dim strFilter As String
-
strFilter = Chr(35) & Replace(Me.txtMemberFilter, ",", Chr(35) & "," & Chr(35)) & Chr(35)
IN() is great if you are looking to match the entire field to a value in a List.
If you are looking to use the LIKE operator, then you'll need to write some code to split the String and build up a WHERE clause, for example: - Public Function parseWhere(ByRef sWHERE As String, ByRef sFieldName As String) As String
-
Dim oWhere As Variant
-
For Each oWhere In Split(sWHERE, ",")
-
parseWhere = parseWhere & " OR " & sFieldName & " LIKE '*" & Trim(oWhere) & "*'"
-
Next oWhere
-
If Len(parseWhere) > 0 Then parseWhere = Right(parseWhere, Len(parseWhere) - 4)
-
End Function
This would be the Immediate Window: - ?parseWhere("one, two, three", "[fldFullName]")
-
[fldFullName] LIKE '*one*' OR [fldFullName] LIKE '*two*' OR [fldFullName] LIKE '*three*'
So the following should work: - Forms.fmTrainingReport.fmMemberItemsReport.Form.Filter = parseWhere(Me.txtMemberFilter, "[fldFullName]"
jforbes, thank you! This works perfectly.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Lyn |
last post by:
Hi,
I am working on a genealogy form. The only table (so far) lists everybody
in the family, one record per person. Each record has an autonum ID.
The parent form (frmMainForm) displays the...
|
by: Heather |
last post by:
I want to create a report that shows the percent of safety audits
complete by each individual shift, based on the safety audit date.
The user has to put in the dates they want the report to pull...
|
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...
|
by: Neal Barney |
last post by:
I have a C program which runs on a device using a Zilog Z180
microprocessor. While it can address 1MB of RAM, it can only address
64KB at any given time. And of that only 16KB can be used for...
|
by: satishrajana |
last post by:
Hi,
My SQL returns a NULL in a datefield if there is no date in that field.
If there is a NULL in this column, I want to replace it with spaces in
my SELECT statement when I am selecting these...
|
by: mrubel99 |
last post by:
I have a subform built into a form in Access 2007. I would like the user to be able to filter the subform using their own criteria by right clicking the column header in the subform. Once the user...
|
by: diogenes |
last post by:
"Rick Brandt" <rickbrandt2@hotmail.comwrote in news:bPnKj.456$%41.325
@nlpi064.nbdc.sbc.com:
I used this approach, and it works a treat!
ID In(SELECT Order_ID FROM orderitems WHERE NAME =...
|
by: rwalle |
last post by:
Hi :
I have done a form with a subform inside to filter customers, the form have a Text box where user write down a name then I take this text box data as a criteria to the query wich subform...
|
by: gershwyn |
last post by:
I have a form (frmEditCategories) containing a sub-form (subCategories) that allows the user to add, delete, and change information that categorizes inventory items. The main form is not bound, and...
|
by: Becker |
last post by:
I have a combo box with a list a values that when picked the form should filter to show the corresponding record from a table. Everything works fine when I open up the form by itself. However, I...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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,...
|
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: 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,...
| |