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

How to apply a subform filter where the filter on field is a partial string of the se

gnawoncents
214 100+
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.

Expand|Select|Wrap|Line Numbers
  1. Forms.fmTrainingReport.fmMemberItemsReport.Form.Filter = "[fldFullName] Like '" & Me.txtMemberFilter & "'"
  2.  
  3. Forms.fmTrainingReport.fmMemberItemsReport.Form.Filter = "*[fldFullName]* Like '" & Me.txtMemberFilter & "'"
  4.  
  5. Forms.fmTrainingReport.fmMemberItemsReport.Form.Filter = "'*' & [fldFullName] & '*' Like '" & Me.txtMemberFilter & "'"
  6.  
Feb 16 '17 #1

✓ answered by Seth Schrock

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
Seth Schrock
2,965 Expert 2GB
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")
Feb 16 '17 #2
gnawoncents
214 100+
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?
Feb 16 '17 #3
Seth Schrock
2,965 Expert 2GB
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.
Feb 16 '17 #4
gnawoncents
214 100+
Thanks for the direction. I wasn't considering an array, but that should work.
Feb 16 '17 #5
Seth Schrock
2,965 Expert 2GB
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.
Expand|Select|Wrap|Line Numbers
  1. Dim strFilter As String
  2. strFilter = Chr(35) & Replace(Me.txtMemberFilter, ",", Chr(35) & "," & Chr(35)) & Chr(35)
Feb 16 '17 #6
jforbes
1,107 Expert 1GB
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:
Expand|Select|Wrap|Line Numbers
  1. Public Function parseWhere(ByRef sWHERE As String, ByRef sFieldName As String) As String
  2.     Dim oWhere As Variant
  3.     For Each oWhere In Split(sWHERE, ",")
  4.         parseWhere = parseWhere & " OR " & sFieldName & " LIKE '*" & Trim(oWhere) & "*'"
  5.     Next oWhere
  6.     If Len(parseWhere) > 0 Then parseWhere = Right(parseWhere, Len(parseWhere) - 4) 
  7. End Function
This would be the Immediate Window:
Expand|Select|Wrap|Line Numbers
  1. ?parseWhere("one, two, three", "[fldFullName]")
  2. [fldFullName] LIKE '*one*' OR [fldFullName] LIKE '*two*' OR [fldFullName] LIKE '*three*'
So the following should work:
Expand|Select|Wrap|Line Numbers
  1. Forms.fmTrainingReport.fmMemberItemsReport.Form.Filter = parseWhere(Me.txtMemberFilter, "[fldFullName]"
Feb 16 '17 #7
gnawoncents
214 100+
jforbes, thank you! This works perfectly.
Feb 16 '17 #8

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

Similar topics

25
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...
2
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...
1
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...
9
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...
10
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...
4
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...
0
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 =...
1
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...
4
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...
18
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...
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:
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
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,...

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.