We help IT Professionals succeed at work.

VB.net BindingSource Filter string

I have a vb.net windows form that uses filters on the on [bindingsource]
I am trying to get a search criteria that is found anywhere in a string to return in the filter results.
[exisiting code] Master_ContactsBindingSource.Filter = "c_company = '" & InStr(CompanySearch.Text, 1) & "'"

The problem is that I receive no results (and does not error either) for terms that I know are in the table such as "son".

The desired result is to return all records that contain the search term "son" but clearly I have this wrong.

c_company = [table field]
[CompanySearch.text] is the textbox with the search term

Thoughts?
Comment
Watch Question

Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
as per the documentation (https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.strings.instr?view=netframework-4.8), your arguments seems to be in an incorrect order. I think it should be:
Master_ContactsBindingSource.Filter = "c_company = '" & InStr(1, CompanySearch.Text) & "'"

Open in new window

Author

Commented:

Eric,


I 'think' I had that sequence somewhere along the way of experimenting. I corrected the string to read as you indicated (I agree with it) however I still receive no results for something such as 'b'. To sort this out I've included some strings that DO work in other areas of the program for reference. I know the field and string captures correctly. The hangup is when I get to the Instr (issue).


Master_ContactsBindingSource.Filter = "c_company like '" & CompanySearch.Text & "*'"
Master_ContactsBindingSource.Filter = "c_address <>''  And  c_company Like '" & CompanySearch.Text & "*'"


I appreciate your help!

Scott

Senior .Net Consultant
Top Expert 2016
Commented:
LOL! you are right, Instr doesn't go there at all!

Author

Commented:

I found that the following works perfect. Boy was I climbing the wrong side of the tree!


Master_ContactsBindingSource.Filter = "c_company like '%" & CompanySearch.Text & "%'"


I get any occurrence of a string which was the goal.


Thank you for the poke Eric!