How to easily filter my query - sql

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

Related

Access search query returning PK not value

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] & "*"));

Query Using Input From A Form with A Checkbox

Thank you, ahead of time, to everyone, for your time and attention.
I am doing a very simple database for my job, that was too large to fit into an excel spreadsheet, which is what we usually use here. I apologize ahead of time, as I have very limited knowledge of access, but have to figure this out.
I have about 1,150,000 records and need to be able to search by the following criteria: Part Number (txtPK), Step (txtStep), Skipped Percentage (txtPer), and Bottleneck? (chkARD); ARD is in the database as "Y" or "N".
This code worked to show either only Y or only N:
IIf([Forms]![Skips_Form]![chkARD],"Y", "N")
However, when I tried to adapt it to show "*" instead of "N" it returns no records, instead of all records.
IIf([Forms]![Skips_Form]![chkARD],"Y", "*")
My desire is to show only the records with "Y" when the checkbox is checked,
and to show all records when it is not checked.
Presumably you are using these iif statements within the where clause of your query, in conjunction with the = operator - something like:
select *
from YourTable
where ARD = IIf([Forms]![Skips_Form]![chkARD],"Y", "*")
If this is indeed the case, then, when the else argument of the iif statement is returned, the SQL statement becomes:
select *
from YourTable
where ARD = "*"
Hence, this will return records for which the ARD field has a literal value of "*".
Instead, you should either use the like operator, which will allow wildcards such as the asterisk to match any value, e.g.:
select *
from YourTable
where ARD like IIf([Forms]![Skips_Form]![chkARD],"Y", "*")
Or, use some simple boolean logic, such as:
select *
from YourTable
where (not [Forms]![Skips_Form]![chkARD]) or ARD="Y"
Complete Working SQL Code
Thank you Lee Mac
SELECT Data.POS, Data.PK, Data.[Step], Data.[ARD?], Skips.Skips, Skips.Total,
Skips.[Skips %], Data.[OPERATION DESCRIPTION], Data.CHARGE_NUMBER, Data.[MDM_PN],
Data.[PlanVer], Data.[PlanRev], Data.[Task_Desc], Data.[Prod Version]
FROM Data INNER JOIN Skips ON Data.POS = Skips.POS
WHERE
(((Data.POS) Like "*" & [Forms]![Skips_Form]![txtPK] & "*")
AND ((Data.[Step]) Like "*" & [Forms]![Skips_Form]![txtStep] & "*")
AND ((Data.[ARD?]) Like IIf([Forms]![Skips_Form]![chkARD],"Y","*"))
AND ((Skips.[Skips %])>=[Forms]![Skips_Form]![txtPer]))
OR
(((Data.POS) Like "*" & [Forms]![Skips_Form]![txtPK] & "*")
AND ((Data.[Step]) Like "*" & [Forms]![Skips_Form]![txtStep] & "*")
AND ((Data.[ARD?]) Like IIf([Forms]![Skips_Form]![chkARD],"Y","*")))
ORDER BY Skips.[Skips %];

Like * does not capture Is Null values

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

How do I query one field from multiple search boxes that might be blank

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.

MS Access multi field search with empty fields

I have a problem very similar to this one, but I just can't seem to solve it!
In MS Access (2003), I want to search a table based on entries in a number of fields, some of which may be empty.
I have:
text fields
date fields
integer fields, and
a memo field (but we can probably not bother searching this one if it is difficult).
They map onto a table exactly.
I am trying to create a query that will return matching rows when data is entered into one or more of these fields, but some fields can be left blank. How the heck do I do this?
A query like the one on the linked question works for text fields, but what do I do about the number fields, date fields (and possibly even the memo field)?
To give a clear example, the following code block works for TextField1, but not NumberField1:
PARAMETERS [Forms]![SearchForm]![FilterTextField1] Text ( 255 ), [Forms]![SearchForm]![FilterNumberField1] Text ( 255 );
SELECT Table1.[TextField1], Table1.[NumberField1], Table1.[TextField2], Table1.[TextField3], Table1.[DateField1], Table1.[DateField2], Table1.[DateField3]
FROM Table1
WHERE (Len([Forms]![SearchForm]![FilterTextField1] & '')=0 OR Table1.[TextField1] Like '*' & [Forms]![SearchForm]![FilterTextField1] & '*') AND (Len([Forms]![SearchForm]![FilterNumberField1] & '')=0 OR Table1.[NumberField1] Like '*' & [Forms]![SearchForm]![FilterNumberField1] & '*');
I do hope you can help. I'm sure I'm missing something really obvious, but for some reason my brain feels like it is leaking out of my ears at the moment.
Thank you!
If you need it, this is the basic design of the relevant entities:
Table1
SomePrimaryKeyWeDontCareAboutRightNow
TextField1
TextField2
TextField3
NumberField1
DateField1
DateField2
DateField3
MemoField1
SearchForm
FilterTextField1
FilterTextField2
FilterTextField3
FilterNumberField1
FilterDateField1
FilterDateField2
FilterDateField3
FilterMemoField1
You can check fo null values or cast to string
You could certainly spend a great deal of time crafting a huge and very hard to debug SQL query for this, or just jump into VBA and write some code to construct just the SQL you need.
VBA is there just for these kinds of scenario, where something is either impossible or becoming too complex to do otherwise.
With VBA, you can use an initial SELECT query that collect all the data, and then construct a WHERE clause based on the content of your search form to filter it.
For instance, I have a form like this, that allows the user to enter any criteria to filter a list of prices:
Some code to implement this could look like:
' Call this whenever the use click the Apply button '
Private Sub btApply_Click()
' Construct the filter '
Dim filter As String
If Not IsBlank(cbSupplierID) Then
If Not IsBlank(filter) Then filter = filter & " AND "
filter = filter & "(SupplierID=" & cbSupplierID & ")"
End If
If Not IsBlank(txtPartNumber) Then
If Not IsBlank(filter) Then filter = filter & " AND "
filter = filter & "(PartNumber LIKE '*" & txtPartNumber & "*')"
End If
If Not ckShowLocked Then
If Not IsBlank(filter) Then filter = filter & " AND "
filter = filter & "(NOT PriceLocked)"
End If
' ... code snipped, you get the jest ... '
' Now re-construct the SQL query '
Dim sql As String
sql = "SELECT * FROM Price"
If Not IsBlank(filter) Then
sql = sql & " WHERE " & filter
End If
SubForm.Form.RecordSource = sql
End Sub
It may seem like a lot of code, but each block only does one thing, and it's a lot easier to debug and maintain than cramming everything into a query.