I have an Access Query that filters a table in my database using
Like "*" & [Forms]![Reports]![Filter] & "*"
which grabs filtered items if that filter is selected, or returns everything if that filter is unselected. However, it misses Null values. If the filter is unselected, the string becomes
Like **
which does not capture Null values.
How can I capture these values in the way I describe?
The above two answers are correct, but to provide some added context NULL cannot be compared to anything. It's not 'not 5', 'not like "**", 'not like "foo"', not 'Date < #12/31/0000 BC#', not False, you get the idea. The only way to pick it off is to do as the above experts state, IS NULL or IS NOT NULL.
You can do this by or'ing the null check so it returns data for both conditions -->
(Like "*" & [Forms]![Reports]![Filter] & "*" OR MyField IS NULL)
Also, you should be able to check the UI is absent a value-->
(Like "*" & [Forms]![Reports]![Filter] & "*" OR [Forms]![Reports]![Filter] = "" )
In Microsoft Access Query criteria, you can use an OR as well:
Like "*" & [Forms]![Reports]![Filter] & "*" Or Is Null
...to have Null not return, then use:
Is Not Null
...or,
Like "*" & [Forms]![Reports]![Filter] & "*" And Is Not Null
Related
I've written a search query with multiple parameters, and parameters are entered via an unbound form. It works except that for one parameter (CostumeInventory.PeriodName) it will return the PK not the value. All parameters & their values come from join tables, and all are set up the same way--datatypes all match, all the same kind of join--all but this one are showing the values instead of the PK like I want them to. Any ideas? Thank you!
Here is my SQL from the query (apologies, I know it's messy, I am just learning so for the most part I'm letting Access write it):
SELECT CostumeInventory.SetID, CostumeInventory.InventoryID,
CostumeInventory.ItemName, CostumeInventory.Description, CostumeInventory.Color,
CostumeInventory.Size, CostumeInventory.ChestBust, CostumeInventory.Waist,
CostumeInventory.Hip, CostumeInventory.Inseam, CostumeInventory.Photo1,
CostumeInventory.Condition, CostumeInventory.RentalRate,
CostumeInventory.ReplacementValue
FROM CostumeInventory
WHERE (((CostumeInventory.Color) Like "*" & [Forms]![frmItemSearch]![cboColor] & "*")
AND ((CostumeInventory.GarmentType) Like "*" & [Forms]![frmItemSearch]![cboSelectType] & "*")
AND ((CostumeInventory.GarmentSubType) Like "*" & [Forms]![frmItemSearch]![cboSubType] & "*")
AND ((CostumeInventory.PeriodName) Like "*" & [Forms]![frmItemSearch]![cboPeriod] & "*"));
I created a query in MS Access that references text put into a form to create an expression for criteria to look up records in a table, by either road name or district.
Others will eventually use the form so I used the like wild card to allow for the flexibility of entering an incomplete road name in the form.
Unfortunately this has meant that if one leaves the road name text box empty, the like function displays all records in the database, and does not limit them based on the second criteria (name of a district.)
Should I be using a different function or perhaps writing more complex criteria?
(I have tried removing the wild card, putting the Or functions in each field on different lines or the same line and also thought about adding to an existing macro to limit the query results by district name, if the street name text box was left blank.)
I have been Googling for quite some time and can't figure this one out. Thank you for any help!
Essentially:
The form (called MJidea) has two text entry boxes -
Street (PriStReport)
District (District)
The query set up:
You could change the SQL where clause of your query to:
where
(
[Forms]![MJidea]![PriStReport] is not null and
[Primary Street] like "*" & [Forms]![MJidea]![PriStReport] & "*"
) or
(
[Forms]![MJidea]![PriStReport] is not null and
[Secondary Street] like "*" & [Forms]![MJidea]![PriStReport] & "*"
) or
(
[Forms]![MJidea]![District] is not null and
[Magisterial District] like "*" & [Forms]![MJidea]![PriStReport] & "*"
)
Alternatively, consolidating the first two tests:
where
(
[Forms]![MJidea]![PriStReport] is not null and
(
[Primary Street] like "*" & [Forms]![MJidea]![PriStReport] & "*" or
[Secondary Street] like "*" & [Forms]![MJidea]![PriStReport] & "*"
)
)
or
(
[Forms]![MJidea]![District] is not null and
[Magisterial District] like "*" & [Forms]![MJidea]![PriStReport] & "*"
)
I have a field called "Expertise" which houses data about Doctors/Nurses/Surgeons such that Doctor1 may have "Pediatric surgeon; Neurology" in this field. Basically just a bunch of information about the Doctor will fill this field. I have four search boxes on a form where users can search for specific health care professionals. Someone may want to search for "pediatric", "Surgeon", and "neurology". I have this set up now using SQL and my code looks like this:
AND (dbo_Contact.Expertise) Like "*" & [Forms]![Data_Qry_Test_Page]![Expertise1] & "*"
AND (dbo_Contact.Expertise) Like "*" & [Forms]![Data_Qry_Test_Page]![Expertise2] & "*"
AND (dbo_Contact.Expertise) Like "*" & [Forms]![Data_Qry_Test_Page]![Expertise3] & "*"
AND (dbo_Contact.Expertise) Like "*" & [Forms]![Data_Qry_Test_Page]![Expertise4] & "*"
Notice how I use Like because the user may want to look up the word "Surg" which will return records that contain surgery AND surgeon. This works great if a record has the words pediatric, surgeon, and neurology in their Expertise field. However I need this query to return Doctor1 who has only surgeon in his Expertise field AND Doctor2 who has neurology in his Expertise field.
If I use OR instead of AND it will return ALL RECORDS in the database. I believe I have to use the Nz() function but I'm not exactly sure how.
You can use Nz to transform a Null text box value to a string which will never match any values stored in dbo_Contact.Expertise. For example, say "BOGUS" will never match. Then Like '*BOGUS*' will also never match.
So you can OR those Nz conditions together. Any non-Null text boxes will select matching rows. Null text boxes will not select any. (And the query result set is the combination of rows selected by each of those conditions.)
SELECT c.*
FROM dbo_Contact AS c
WHERE
c.Expertise Like '*' & Nz(Forms!Data_Qry_Test_Page!Expertise1, 'BOGUS') & '*'
OR c.Expertise Like '*' & Nz(Forms!Data_Qry_Test_Page!Expertise2, 'BOGUS') & '*'
OR c.Expertise Like '*' & Nz(Forms!Data_Qry_Test_Page!Expertise3, 'BOGUS') & '*'
OR c.Expertise Like '*' & Nz(Forms!Data_Qry_Test_Page!Expertise4, 'BOGUS') & '*'
I think your problem is just in the organization of your boolean logic.
The way your query works now, you would only get a result if dbo_Contact.Expertise exists in all 4 fields.
What you want however is if Expertise exists in field1 or field2 or field3 or field4.
The reason why you are getting the whole database currently is because you have a few criteria above this group that you've not included here. Its saying "All this stuff above" OR "check this in expertise"
Instead you need to include it as one group anding it to the others above.
AND ((dbo_Contact.Expertise) Like "*" & [Forms]![Data_Qry_Test_Page]![Expertise1] & "*"
OR (dbo_Contact.Expertise) Like "*" & [Forms]![Data_Qry_Test_Page]![Expertise2] & "*"
OR (dbo_Contact.Expertise) Like "*" & [Forms]![Data_Qry_Test_Page]![Expertise3] & "*"
OR (dbo_Contact.Expertise) Like "*" & [Forms]![Data_Qry_Test_Page]![Expertise4] & "*")
Notice how I have put all the constraints in parens and "anded" them together with the previous constraints.
I am trying to filter a query via a textbox on a form. I have this in the criteria of the query:
Like "*" & [Forms]![Form_Name]![Textbox] & "*"
However if the textbox is blank it only shows data in the query that have got text in the field. It doesn't show all data, i.e. the fields that have no data. Is this possible to do?
The only way I have found to do it is to have 2 queries 1 with the filter and 1 without and run code that if the textbox is empty swap the query.
Just append a zero-length string to the field for the compare, then none of the fields will be null, so they will match *.
WHERE MyField & ""
Like "*" & [Forms]![Form_Name]![Textbox] & "*"
I don't have a copy of Access available to me at the moment but a quick google and some tinkering tells me that this is likely to do what you need:
Like "*" & [Forms]![Form_Name]![Textbox] & "*"
Or (Len(Nz([Forms]![Form_Name]![Textbox],"")) = 0 And Len(Nz([Column],"")) = 0)
Sources:
http://bytes.com/topic/access/answers/607087-validation-if-null-empty-string
http://office.microsoft.com/en-001/access-help/table-of-operators-HA010235862.aspx
http://www.techonthenet.com/access/functions/advanced/nz.php
I'm trying to get a query working that takes the values (sometimes just the first part of a string) from a form control. The problem I have is that it only returns records when the full string is typed in.
i.e. in the surname box, I should be able to type gr, and it brings up
green
grey
graham
but at present it's not bringing up anything uless the full search string is used.
There are 4 search controls on the form in question, and they are only used in the query if the box is filled in.
The query is :
SELECT TabCustomers.*,
TabCustomers.CustomerForname AS NameSearch,
TabCustomers.CustomerSurname AS SurnameSearch,
TabCustomers.CustomerDOB AS DOBSearch,
TabCustomers.CustomerID AS MemberSearch
FROM TabCustomers
WHERE IIf([Forms]![FrmSearchCustomer]![SearchMember] Is Null
,True
,[Forms]![FrmSearchCustomer]![SearchMember]=[customerid])=True
AND IIf([Forms]![FrmSearchCustomer].[SearchFore] Is Null
,True
,[Forms]![FrmSearchCustomer]![SearchFore] Like [customerforname] & "*")=True
AND IIf([Forms]![FrmSearchCustomer]![SearchLast] Is Null
,True
,[Forms]![FrmSearchCustomer]![SearchLast] Like [customersurname] & "*")=True
AND IIf([Forms]![FrmSearchCustomer]![Searchdate] Is Null
,True
,[Forms]![FrmSearchCustomer]![Searchdate] Like [customerDOB] & "*")=True;
There is an Access Method for that!
If you have your "filter" controls on the form, why don't you use the Application.buildCriteria method, that will allow you to add your filtering criterias to a string, then make a filter out of this string, and build your WHERE clause on the fly?
selectClause = "SELECT TabCustomers.* FROM TabCustomers"
if not isnull(Forms!FrmSearchCustomer!SearchMember) then
whereClause = whereClause & application.buildCriteria(your field name, your field type, your control value) & " AND "
endif
if not isnull(Forms!FrmSearchCustomer!SearchFore) then
whereClause = whereClause & application.buildCriteria(...) & " AND "
endif
if not isnull(Forms!FrmSearchCustomer!SearchLast) then
whereClause = whereClause & application.buildCriteria(...) & " AND "
endif
if not isnull(Forms!FrmSearchCustomer!SearchDate) then
whereClause = whereClause & application.buildCriteria(...) & " AND "
endif
--get rid of the last "AND"
if len(whereClause) > 0 then
whereClause = left(whereClause,len(whereClause)-5)
selectClause = selectClause & " WHERE " & whereClause
endif
-- your SELECT instruction is ready ...
EDIT: the buildCriteria will return (for example):
'field1 = "GR"' when you type "GR" in the control
'field1 LIKE "GR*"' when you type "GR*" in the control
'field1 LIKE "GR*" or field1 like "BR*"' if you type 'LIKE "GR*" OR LIKE "BR*"' in the control
PS: if your "filter" controls on your form always have the same syntax (let's say "search_fieldName", where "fieldName" corresponds to the field in the underlying recordset) and are always located in the same zone (let's say formHeader), it is then possible to write a function that will automatically generate a filter for the current form. This filter can then be set as the form filter, or used for something else:
For each ctl in myForm.section(acHeader).controls
if ctl.name like "search_"
fld = myForm.recordset.fields(mid(ctl.name,8))
if not isnull(ctl.value) then
whereClause = whereClause & buildCriteria(fld.name ,fld.type, ctl.value) & " AND "
endif
endif
next ctl
if len(whereClause)> 0 then ...
You have your LIKE expression backwards. I have rewritten the query to remove the unnecessary IIF commands and to fix your order of operands for the LIKE operator:
SELECT TabCustomers.*
FROM TabCustomers
WHERE (Forms!FrmSearchCustomer!SearchMember Is Null Or Forms!FrmSearchCustomer!SearchMember=[customerid])
And (Forms!FrmSearchCustomer.SearchFore Is Null Or [customerforname] Like Forms!FrmSearchCustomer!SearchFore & "*")
And (Forms!FrmSearchCustomer!SearchLast Is Null Or [customersurname] Like Forms!FrmSearchCustomer!SearchLast & "*")
And (Forms!FrmSearchCustomer!Searchdate Is Null Or [customerDOB] Like Forms!FrmSearchCustomer!Searchdate & "*");
I built that query by replicating the most likely circumstance: I created a dummy table with the fields mentioned and a form with the fields and a subform with the query listed above being refreshed when the search button was pushed. I can provide a download link to the example I created if you would like. The example works as expected. J only picks up both Jim and John, while John or Jo only pulls the John record.
Two things are going on - the comparisions should be reversed and you are not quoting strings properly.
It should be [database field] like "partial string + wild card"
and all strings need to be surrounded by quotes - not sure why your query doesn't throw errors
So the following should work:
,[customerforname] Like """" & [Forms]![FrmSearchCustomer]![SearchFore] & "*""" )=True
Note the """" that is the only way to append a single double-quote to a string.
My only thoguht is that maybe a () is needed to group the like
For example a snippet on the first part
,[Forms]![FrmSearchCustomer]![SearchFore] Like ([customerforname] & "*"))=True
It has been a while since I've used access, but it is the first thing that comes to mind
This is a complete re-write to allow for nulls in the name fields or the date of birth field. This query will not fail as too complex if text is entered in the numeric customerid field.
SELECT TabCustomers.CustomerForname AS NameSearch, TabCustomers.CustomerSurname AS SurnameSearch, TabCustomers.CustomerDOB AS DOBSearch, TabCustomers.customerid AS MemberSearch
FROM TabCustomers
WHERE TabCustomers.customerid Like IIf([Forms]![FrmSearchCustomer].[Searchmember] Is Null,"*",[Forms]![FrmSearchCustomer]![Searchmember])
AND Trim(TabCustomers.CustomerForname & "") Like IIf([Forms]![FrmSearchCustomer].[SearchFore] Is Null,"*",[Forms]![FrmSearchCustomer]![SearchFore] & "*")
AND Trim(TabCustomers.CustomerSurname & "") like IIf([Forms]![FrmSearchCustomer].[Searchlast] Is Null,"*",[Forms]![FrmSearchCustomer]![SearchLast] & "*")
AND (TabCustomers.CustomerDOB Like IIf([Forms]![FrmSearchCustomer].[SearchDate] Is Null,"*",[Forms]![FrmSearchCustomer]![SearchDate] ) Or TabCustomers.CustomerDOB Is Null)