Using "Is Null" as a criteria based on a form - sql

I'm trying to get my query to only return null records if a radio box on a form is ticked. If I manually include the criteria Is Null in the query it works just fine, and elsewhere things like
IIf([Forms]![Reports]![Status - Active]=True,"Active")
work just fine.
However,
IIf([Forms]![Reports]![Status - Null]=True,"Is Null")
just ignores it (I assume because it's actually looking for the text "Is Null" rather than null records) and
IIf([Forms]![Reports]![Status - Null]=True,Is Null)
fails because it's too complex. Is there any way of achieving what I'm trying to achieve?
Thanks.

You could work around in a couple of ways, for example.
SELECT t.atext
FROM Table1 t
WHERE IsNull([atext])=[Forms]![Reports]![Status - Null]
BTW, Immediate If (IIf) takes three arguments.

Related

SQL Selection Query getting corrupted

Ive come across a very unusual problem (for me at least) and I have no idea how to solve it.
Essentially I made a really simple selection query to search our clients in a table (dbo_t_Person) and return their records. I needed them to be searchable even if we only have an email address, or phone number for some clients on hand. Therefore I wrote the criteria to either ignore a field if no data was entered, or to search similar (via 'Like') if only partial details were entered into any given field. See the SQL below, apologies for how repetitive it is.
This is all well and good, it works perfectly and is fast enough for our uses.
However.
I can run the query as many times as I wish with new data entered and it works fine, but if I close the query and reopen it, the SQL goes haywire and it runs out of memory and crashes access, this is crashing just opening the SQL as well as running it. By haywire I mean that if i manage to luck out and reopen the SQL, lines of SQL are suddenly copied endlessly on the page.
This happens every time I rewrite the SQL from scratch, how the hell do I stop this happening?
Here is the working clean code:
SELECT dbo_t_Person.PersonID
,dbo_t_Person.FullName
,dbo_t_Person.Address1
,dbo_t_Person.Address2
,dbo_t_Person.City
,dbo_t_Person.Zip
,dbo_t_Person.STATE
,dbo_t_Person.Country
,dbo_t_Person.Mobile
,dbo_t_Person.Phone
,dbo_t_Person.Email
FROM dbo_t_Person
WHERE (
(
(dbo_t_Person.PersonID) = [Forms]![from MICHAEL TEST WORKING]![OwnerIDEntry]
OR [Forms]![from MICHAEL TEST WORKING]![OwnerIDEntry] IS NULL
)
AND (
(dbo_t_Person.FullName) LIKE "*" & [Forms]![from MICHAEL TEST WORKING]![NameEntry] & "*"
OR [Forms]![from MICHAEL TEST WORKING]![NameEntry] IS NULL
)
)
And so on for the remaining entry fields
However if I can get the SQL back open again it it appears thousands of lines of
Or [Forms]![from MICHAEL TEST WORKING]![NameEntry] Is Null
for all entry fields is endlessly repeated.
Something is making the code copy end on end, how do I stop it?
Consider an adjusted WHERE clause with NZ() to handle if controls are empty or not.
WHERE dbo_t_Person.PersonID = NZ([Forms]![from MICHAEL TEST WORKING]![OwnerIDEntry],
dbo_t_Person.PersonID)
AND dbo_t_Person.FullName = LIKE "*" & NZ([Forms]![from MICHAEL TEST WORKING]![NameEntry],
dbo_t_Person.FullName) & "*"
Try changing your criteria to be more efficient and clean, like this:
IIF(ISNULL([Forms]![from MICHAEL TEST WORKING]![OwnerIDEntry]),TRUE,PersonID=[Forms]![from MICHAEL TEST WORKING]![OwnerIDEntry])
Since you are only dealing with a single table you can also do away with dbo_t_Person. from everywhere, like this:
SELECT PersonID,FullName,Address1,Address2,City,Zip,STATE,Country,Mobile,Phone,Email
FROM dbo_t_Person
Maybe the simplified version of the SQL will stop Access from corrupting it.

How can I write a WHERE statement that looks for a variable OR Null?

I am attempting to write a query for an Altiris report. This query is looking for machine information. The query works fine, however the problem I am running into is with my parameters. I have set up multiple parameters within Altiris to allow me to filter and search through the report for multiple fields. Then, in my query, I add those parameters into the WHERE statements.
All of the parameters were working fine, until I added Make and Model parameters. We have quite a few machines that do not have information populated into these fields. So when I add in the WHERE xxxx LIKE N'%Make%', I lose about 500 machines based on it now only looking for machines with something in that field. I tried to fix this by adding lines like the following:
Where ((xxxx LIKE N'%Make%' OR xxxx is null))
This kind of worked, in that now the report shows all machines... But if I enter "HP" into the Make parameter field and then rerun the report... it shows all HP machines like I want, but also all of the null machines as well.
How can I rewrite my where statements so that they do not exclude machines in the report, and allow me to filter by all HP machines, without showing null values as well?
Hope this made sense, and thank you
In this snip of code, the last two lines make me lose about 500 machines in my total machine count of the report. It is omitting all machines that have null values.
WHERE
(dbo.OS_Version.[OS Name] LIKE N'%OSName%') AND
(dbo.OS_Version.[OS Version] LIKE N'%Build%') AND
(dbo.OS_Version.Name LIKE N'%Name%') AND
(dbo.Inv_AeX_AC_Identification.[Hardware Serial Number] LIKE N'%Serial%') AND
(dbo.vHWComputerSystem.Manufacturer LIKE N'%Make%') AND
(dbo.vHWComputerSystem.Model LIKE N'%Model%')
This is how I tried to fix it, and now I get all 20,000 machines. But my make/model fields report on null fields as well.
WHERE
(dbo.OS_Version.[OS Name] LIKE N'%OSName%') AND
(dbo.OS_Version.[OS Version] LIKE N'%Build%') AND
(dbo.OS_Version.Name LIKE N'%Name%') AND
(dbo.Inv_AeX_AC_Identification.[Hardware Serial Number] LIKE N'%Serial%') AND
((dbo.vHWComputerSystem.Manufacturer LIKE N'%Make%') OR (dbo.vHWComputerSystem.Manufacturer is null)) AND
((dbo.vHWComputerSystem.Model LIKE N'%Model%') OR (dbo.vHWComputerSystem.Model is null))
I'm guess that if you don't enter a value for a parameter, it's coming through as an empty string, and of course, every varchar is LIKE '%%'.
I'm not sure what RDBMS this is, but if the ISNULL function is available, try this:
where ((ISNULL(xxxx,'') LIKE N'%Make%')
This replaces nulls with the empty string before doing the LIKE comparison.
I think you want something like this:
(cs.Manufacturer LIKE N'%#Make%' OR #Make = '') AND
(cs.Model LIKE N'%#Model%' OR #Model = '')
I am using = '' rather than IS NULL because you are clearly not passing in the parameters as NULL values (the LIKE wouldn't work).
This does not provide a method for filtering to get only the NULL values, because you are using the "special value" for the parameter to mean "don't apply a filter here".
Note that cs is intended as a table alias. I also strongly recommend that you use table aliases so your queries are easier to write and to read.
I think you're looking for something like WHERE ISNULL(Model, '') LIKE '%Model%'. However, you should replace '%Model%' with a variable. The above example would literally match the word 'Model'
DECLARE #Model NVARCHAR(100) = 'T-800'
...
WHERE ISNULL(Model, '') LIKE '%' + #Model + '%'
^ This would not include rows with NULL Model values

Mixing 'Like' with comparators in an iif statement?

I'm attempting to use the query builder to formulate a query based on user input on a form, but I'm running into an issue.
I've been using this code to filter and check for null/"ALL" field before which is working fine.
Like IIf([Forms]![TransactionsForm]![ComboActStatus]="ALL","*",
[Forms]![TransactionsForm]![ComboActStatus])
But I run into an issue when I want to do the same thing with fields that signify a range. I attempted this:
IIf([forms]![TransactionsForm]![txtAmountFrom] Is Null Or
[forms]![TransactionsForm]![txtAmountTo] Is Null,
([dbo_customerQuery].[amount]) Like "*",
([dbo_customerQuery].[amount])>=[forms]! [TransactionsForm]![txtAmountFrom] And
([dbo_customerQuery].[amount])<=[Forms]![TransactionsForm]![txtAmountTo])
But it's causing my entire query to fail. How can I do this similar thing? Use "Like *" in the null case (return everything), but use comparators rather than "like" statements in the second case?
Unless I'm missing something LIKE "*" will return true for all values, so this should work:
IIf([forms]![TransactionsForm]![txtAmountFrom] Is Null Or
[forms]![TransactionsForm]![txtAmountTo] Is Null,
true,
([dbo_customerQuery].[amount])>=[forms]! [TransactionsForm]![txtAmountFrom] And
[dbo_customerQuery].[amount])<=[Forms]![TransactionsForm]![txtAmountTo])
)
The code that finally worked for me, and didn't have Access split it into separate lines was:
>=IIf([forms]![TransactionsForm]![txtAmountFrom] Is Null,0,[forms]![TransactionsForm]!
[txtAmountFrom]) And <=IIf([forms]![TransactionsForm]![txtAmountTo] Is Null,9999999999,
[forms]![TransactionsForm]![txtAmountTo])

Rails select with include statement

I've been trying to find a proper solution for this problem but didn't succeed. I know that we can't do select with include statement.
For example I have a model called Parent which have many children. I tried following things
1) When I tried this
Parent.includes(:children).select("parent.name, children.age")
Rails completely ignores the select clause.
2) Then I tried this
Parent.joins(:children).select("parent.name, children.age")
The select clause works but instead of returning a nested object it returns me a flat array of objects. So I have to again run a group by command on it to make it nested.
3) I found something called preload, but again not enough documentation for it.
I'm tired of finding a solution to this problem. Can someone point me in a right direction.
===================================================================
By nested object I meant I should be able to do things like
#parents.each do |parent|
puts parent.name
parent.children.each do |child|
puts child.age
end
end
I can achieve this with include but then it selects all attributes which are not needed.

The right way to prepare SQL statements with parametrized text search

Suddenly I've realized that while this works in groovy just like it is expeceted:
Sql.newInstance(connectionParams).rows("SELECT FROM ITEMS WHERE id = ?", [200])
this won't work
Sql.newInstance(connectionParams).rows("SELECT FROM ITEMS WHERE name LIKE '%?%'", ["some"])
All you can get is
Failed to execute: SELECT FROM ITEMS WHERE name LIKE '%?%' because:
The column index is out of range: 1, number of columns: 0.
My questions are:
Is it intentionally implemented this way? I've never needed to have a parametrized text search, so I'm not sure where this behaviour is typical or not.
How can I nevertheless safely parametrize statement with text search in it?
I believe you want to include the %'s in the parameter, like:
Sql.newInstance(connectionParams).rows("SELECT FROM ITEMS WHERE name LIKE ?", ["%some%"])