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

Using VBA to filter many fields for a keyword

112 100+
I am trying to create button that runs a filter in VBA that search several fields (Combo boxes and memo fields) for a key word. I started off just filtering one combo box with only a single word in it using this code and it works!
Expand|Select|Wrap|Line Numbers
  1. Private Sub Apply_Filter_Click()
  2. Dim MyValue As String
  3. MyValue = InputBox("Enter Keyword")
  4. Me.Filter = "[Field1] = '" & MyValue & "'"
  5. Me.FilterOn = True
  6. End Sub
  7.  
I am now attempting to filter a memo field but can’t get the code to work? See Below.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Apply_Filter_Click() 
  2. Dim MyValue As String 
  3. MyValue = InputBox("Enter Keyword") 
  4. Me.Filter = "[Field1] = '*" & MyValue & "*'"
  5. Me.FilterOn = True 
  6. End Sub 
  7.  
I have tried several different variation of this code with the * in a different spot and I either get errors or no results? Anyone see what I am doing wrong? And how do I add more fields on to the filter? Do I just use "Or" and repeat the code?
Jun 17 '13 #1

✓ answered by TheSmileyCoder

The or you have in your current syntax is placed between the evaluation strings, not as part of it.

Try this:
Expand|Select|Wrap|Line Numbers
  1. Me.Filter = "[Field1] Like '*" & MyValue & "*' Or [Field2] Like '*" & MyValue & "*'"

5 13621
TheSmileyCoder
2,322 Expert Mod 2GB
Filters are basicly a WHERE clause with the Where keyword. So if what you write won't work in the query designer it won't work in the filter either.

Now when using wildcards you can not use the equality (=) operator, but must use the LIKE keyword instead:
Expand|Select|Wrap|Line Numbers
  1. Me.Filter = "[Field1] Like '*" & MyValue & "*'"
Moving on, yes, you can just add more fields to your code using OR between each comparison.
Jun 17 '13 #2
Redbeard
112 100+
Thanks TheSmilyCoder that worked!
However, I am having trouble with adding additional fields with "or"? I thought it would be simple as just adding the "or" in-between statements but I am obviously missing something as I get a Type Mismatch Error 13. What am I missing?
Expand|Select|Wrap|Line Numbers
  1. Me.Filter = "[Field1] Like '*" & MyValue & "*'" Or "[Field2] Like '*" & MyValue & "*'"
Jun 18 '13 #3
TheSmileyCoder
2,322 Expert Mod 2GB
The or you have in your current syntax is placed between the evaluation strings, not as part of it.

Try this:
Expand|Select|Wrap|Line Numbers
  1. Me.Filter = "[Field1] Like '*" & MyValue & "*' Or [Field2] Like '*" & MyValue & "*'"
Jun 18 '13 #4
Redbeard
112 100+
It worked! I am still trying to learn how VBA works and it is hard to wrap my head around the syntax. Thanks again TheSmilyCoder for all the help!
Jun 19 '13 #5

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

Similar topics

7
by: Peter Otten | last post by:
I'm sure they have been mentioned somewhere but here are some more advantages of a decorator keyword (I use "transform"): - The docstring can be moved to the top of the decorator suite. - Simple...
3
by: Derek Basch | last post by:
Is it bad form to use the global window variable to reference an event handlers window? Like so: function SortableTable() { oFilterAdd = this.document.createElement("button");...
0
by: CSDunn | last post by:
Hello, I have a problem with field filtering between an Access 2000 Project form (the application is called CELDT), and the report that shows the results of the filter. Both the form and the...
8
by: dick | last post by:
I am just trying to print/report the results of a "filter by selection" which is done by right-clicking a form, filling in values, and "applying the filter." I have searched the newsgroups, and...
3
by: CSDunn | last post by:
Hello, I have 14 fields on a report that hold integer values. The field names use the following naming convention: T1Number, T2Number ....T14Number. I need to get a 'sub total' of all fields as...
1
by: Miguel Dias Moura | last post by:
Hello, What I know: To send a Value in the URL and filter the results in order to display only the database records which FIELD_A = Value. What I need to do: I have a page with an Input...
2
by: jcf378 | last post by:
hi all. I have a form which contains a calculated control ("days") that outputs the # of days between two dates (DateDiff command between the fields and ). However, when I click "Filter by...
3
by: Redbeard | last post by:
Hi All this is my first time post, be gentle. I am looking at creating a keyword search that searches multiple fields in a Form and then filters records that match the keyword. The Form...
3
by: Soulspike | last post by:
Form name to filter = frmSortFor Filter based on list box from frmTest= lstSortFor Form containing list box = frmTest Field (CompCodes) data format = "PM SM TS EW WA" I have a database that I...
5
aas4mis
by: aas4mis | last post by:
I haven't had much luck with specific controls, their properties and loops in the past. I thought I would share this tidbit of code, feel free to modify/modularize in any way to suit your needs. This...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.