Access Tips Query and Filter Criteria
Access Tips Query and Filter Criteria
11/15/2013
| Home
Excel
Matching Text
When you enter text into the criteria cell your text should be enclosed in quotes ("") to distinguish it from other expressions and operators that you may need to add. "Text" To match a word or phrase simply type the text you want to match. The query will find all the records that match the text exactly. Access will add the quote marks at each end. It is only necessary to enter the quotes yourself if you type text that might confuse the query. For example you may want to type a phrase that contains the words "and" or "or". Access would normally interpret these words as instructions. You can manually insert the quote marks at each end of the phrase to make sure the criterion means what you intend it to. This example will display all the records that contain the entry London in the Town field. "Text" Or "Text" To match one of two or more words or phrases , type the text you want to match separated by the word "or ". The query will find all the records that match any of the words or phrases. Enter quote marks yourself if you think the text might confuse the query. This example will display all the records that contain either London or Paris in the Town field.
"Text" "Text" To match one of several words or phrases , you can type each word or phrase in a new row moving down the column. This gives the same result as using "or " but has the advantage that your criteria might be easier to read. This example will display all the records that contain the entry London, Paris or Amsterdam in the Town field. Note: If this method is combined with criteria for other fields those criteria must be repeated for each row. In ("Text", "Text", "Text") To match a word or phrase from a list, type the list items separated by commas, and enclose the list in round brackets (parentheses). Access will add the expression "In " and place quote marks where needed - you can do this manually if you wish. This example will display all the records that contain UK or USA or France in the Country field. Not "Text" To exclude a word or phrase , use the expression "Not" followed by the word of phrase you want to exclude (enclosed in quotes). This example will display records that contain anything other than London in the Town field.
http://www.fontstuff.com/access/acctut06.htm
1/5
11/15/2013
Not In ("Text", "Text", "Text") To exclude a list of words or phrases from the search use the same method as for matching from a list but add the expression "Not" at the beginning. This example will display all records that contain anything other than UK or USA or France in the Country field. ^ top
Using Wildcards
A wildcard is a special character that can stand for either a single character or a string of text. Wildcards are useful when you want the query to look for a range of different possible values, and also when you are not certain exactly what you are looking for but can give the query some clues to work with. The two wildcards we commonly use are the asterisk or star (* ) and the question mark (? ).The asterisk (* ) represents any string of text from nothing up to an entire paragraph or more. The question mark (? ) represents a single character only (although you could use, for example, two question marks to represent two unknown characters). For example: Yor* would find York , Yorkshire and Yorktown but not New York . Mar? would find Mark but not Mario , Martin or Omar . F*d would find Fred and Ferdinand but not Frederick .
Like "Text*" To match text starting with a particular letter or string type the letter or string of text followed by an asterisk. Access will add the expression "Like " and place quotes around your typing. This example will display all records that have an entry starting with S in the Company field.
Like "*Text" To match text ending with a particular letter or string type an asterisk followed by a letter or string of text. This example will display all records that have an entry ending with Plc in the Company field.
Like "[Letter-Letter]*" To match text starting with letters within a certain range you must type the entire expression as shown (this one is too complicated for Access to work out what you want. This example will display all the records with entries starting with the letters A - D in the Company field. You can often get the same results by using mathematical operators such as greater than (>) and less than (<). These are normally used for specifying numbers and dates but can also be used for text. For example: <"N" would find all entries beginning with a letter lower than the letter N in the alphabet. In other words, all entries starting with the letters A - M. >"F" And <"H" would find all entries beginning with the letters F and G . ^ top
<X
http://www.fontstuff.com/access/acctut06.htm 2/5
11/15/2013
<X To find values less than a certain number type a less than sign (<) followed by the number. This example will display all records with an entry less than 1000 in the CustomerNumber field.
Between X And Y To find values in a range of numbers type the expression shown where X and Y represent the numbers at opposite ends of the range. This example will display all records with entries falling within the range 500-700 in the CustomerNumber field. ^ top
=Date() To match today's date type the expression shown. Date() means "today ". This example will display all the records with entries for the current date in the Invoice Date field.
Year([Fieldname])=Year(Now()) To match the current year type the expression shown, entering the name of the current field in square brackets where indicated. This example will display all the records with entries for the current year in the Invoice Date field.
Year([Fieldname])=Year To match a particular year type the expression shown, entering the name of the current field in square brackets where indicated and the required year in place of Year. This example will display all the records with a date in 1998 in the Invoice Date field.
<Date()-30 To match a particular calculated date range you will need to use a combination of expressions. This expression employs a calculation that subtracts 30 from the current date and also includes the less than operator. This example will display all the records with a date more than 30 days old in the Invoice Date field. ^ top
Excluding Things
Sometimes you want to specifically exclude criteria from your search. This is done with the expression Not. This expression can be used on its own or in combination with other expressions. For example: Not "text" finds all records except those matching the specified text. Not Like "X*" finds all records except those starting with the specified letter (or
http://www.fontstuff.com/access/acctut06.htm 3/5
11/15/2013
string of text). Here are some more examples: Not "Text" To exclude specific records from the search use the expression Not followed by the text which matches those records you want left out. The text needs to be between quotes as shown here - Access will normally do that for you. This example will find all records for contacts in towns other than London. Not Like "Text*" You can use wildcards with the Not expression, which then becomes Not Like followed by your wildcard criteria. Here is just one example. This example will find all records for contacts in towns starting will letters other than L.
And Not "Text" The Not expression can be used in combination with other expressions, when it becomes And Not followed by the text you want to exclude from your search. This example will find all records for contacts in towns starting with the letter L but will exclude those in London. ^ top
Is Not Null To find fields that are not empty use the Is Not Null expression. This looks for fields that contain data. If there is something in the field the record will be shown. Note that Is Not Null will find fields containing zero length entries. (If you want to leave them out try excluding them with the And Not expression.) This example finds all records for contacts whose fax number has been recorded. "" To find zero length entries use "" expression. This looks for zero length entries in the specified field. This example would find, depending on why you had made use of the zero length entry feature, all records for contacts who did not have a fax.
^ top As I said at the beginning, this is not an exhaustive list of query criteria. Many of these expressions can be combined to create more complex criteria. You can use calculations to construct criteria. The scope is almost limitless. Use your imagination and see what you get! Above all, remember that Access is logical. If you don't get the result you were expecting, read the grid a line at a time (which is what Access does) and see if it makes sense.
http://www.fontstuff.com/access/acctut06.htm
4/5
11/15/2013
Sometimes it helps to go and check out the SQL (the language Access uses to write the query - SQL stands for Structured Query Language ). You can view the SQL by clicking the SQL View button on the toolbar. ^ top
http://www.fontstuff.com/access/acctut06.htm
5/5