IIf function inside Access query - sql

I have a query and I would like to use an IIf function as part of the criteria. Here is the full SQL:
SELECT Hits.HitID, Hits.ListingID, Hits.HitCount, Hits.HitDate, Hits.HitTypeID, Hits.IsDeleted
FROM Hits
WHERE (((Hits.HitDate)>=[Forms]![frmReports]![txtStartDate]) AND ((Hits.IsDeleted)="N"));
Here is the piece of code that causing me anguish:
>=[Forms]![frmReports]![txtStartDate]
If I have a date on frmReports, this will work fine; however, if no date is entered, this returns 0 records (I want it to return ALL records, if this is the case).
How can I make this work?

Try this:
SELECT Hits.HitID, Hits.ListingID, Hits.HitCount, Hits.HitDate, Hits.HitTypeID, Hits.IsDeleted
FROM Hits
WHERE (((Hits.HitDate)>=nz([Forms]![frmReports]![txtStartDate],"1/1/1")) AND ((Hits.IsDeleted)="N"));
or this
SELECT Hits.HitID, Hits.ListingID, Hits.HitCount, Hits.HitDate, Hits.HitTypeID, Hits.IsDeleted
FROM Hits
WHERE (((Hits.HitDate)>=[Forms]![frmReports]![txtStartDate]) AND ((Hits.IsDeleted)="N"))
OR (([Forms]![frmReports]![txtStartDate] = "") AND (Hits.IsDeleted="N"));

Related

Why does this access query work and the other does not?

I have a query that filters based on a boolean value on a form (Task Archived). The boolean value is stored as a long in the underlying table (only 0 and -1 are allowed).
I wanted to write a query that would either show only those records that are false (records not archived) or all records.
I would have expected the follwing query to work, but it does not... it returns only those records for which the archive field is set to True (-1):
SELECT tbl_Tasks.TaskID, tbl_Tasks.TArchive
FROM tbl_Tasks
WHERE (((tbl_Tasks.TArchive)=
IIf([Forms]![frm_Activity]![txtArchivedTaskDisplay]=0,False,
(tbl_Tasks.TArchive)=0 Or (tbl_Tasks.TArchive)=-1)));
The query below, which I would expect to show only those records that are True, in fact returns all records:
SELECT tbl_Tasks.TaskID, tbl_Tasks.TArchive
FROM tbl_Tasks
WHERE (((tbl_Tasks.TArchive)=
IIf([Forms]![frm_Activity]![txtArchivedTaskDisplay]=0,False,
(tbl_Tasks.TArchive)<>0)));
Why is this? What logic is access follwing here?
You cannot put SQL where condition within IIF. Anything within IIF will be evaluated if possible. Since you only have 0 or -1 below evaluation will always be true.
((tbl_Tasks.TArchive)=0 Or (tbl_Tasks.TArchive)=-1) => true
that's why you are only seeing TArchive = true
You could try something like this for your first query:
WHERE
tbl_Tasks.TArchive **<** IIf(displayArchivedOnly,0,1);
for the second one:
WHERE
tbl_Tasks.TArchive = [Forms]![frm_Activity]![txtArchivedTaskDisplay]
assuming your txtArchivedTaskDisplay holds either true or false value like a toggle button. but again, read more about how iif works.

Date comparison on Kayako Query Language - KQL

I am using Kayako Querying Language and trying to get the query to return all closed/resolved, closed/unresolved, and review tickets from a specified month. When I run the query, every ticket ever comes up, and it seems to ignore the date function I am using. What am I doing wrong?
SELECT COUNT(*) AS Total
FROM ‘Tickets'
WHERE ‘Tickets.Creation Date’ = month(June 2016) AND ‘Tickets.Status’ = ‘Closed’ OR ‘Tickets.Status’ = ‘Resolved’ OR ‘Tickets.Status’ = ‘Unresolved'
GROUP BY ‘Tickets.Status'
Thanks.
Replace the date comparison in your WHERE clause for this:
WHERE 'Tickets.Creation Date':Month = 6 and 'Tickets.Creation Date':Year = 2016
Be also careful with the quotes, you sometimes use ‘ and others '

Logical operator sql query going weirdly wrong

I am trying to fetch results from my sqlite database by providing a date range.
I have been able to fetch results by providing 3 filters
1. Name (textfield1)
2. From (date)(textfield2)
3. To (date)(textfield3)
I am inserting these field values taken from form into a table temp using following code
Statement statement6 = db.createStatement("INSERT INTO Temp(date,amount_bill,narration) select date,amount,narration from Bills where name=\'"+TextField1.getText()+"\' AND substr(date,7)||substr(date,4,2)||substr(date,1,2) <= substr (\'"+TextField3.getText()+"\',7)||substr (\'"+TextField3.getText()+"\',4,2)||substr (\'"+TextField3.getText()+"\',1,2) AND substr(date,7)||substr(date,4,2)||substr(date,1,2) >= substr (\'"+TextField2.getText()+"\',7)||substr (\'"+TextField2.getText()+"\',4,2)||substr (\'"+TextField2.getText()+"\',1,2) ");
statement6.prepare();
statement6.execute();
statement6.close();
Now if i enter the following input in my form for the above filters
1.Ricky
2.01/02/2012
3.28/02/2012
It fetches date between these date ranges perfectly.
But now i want to insert values that are below and above these 2 date ranges provided.
I have tried using this code.But it doesnt show up any result.I simply cant figure where the error is
The below code is to find entries having date lesser than 01/02/2012 and greater than 28/02/2012.
Statement statementVII = db.createStatement("INSERT INTO Temp5(date,amount_rec,narration) select date,amount,narration from Bills where name=\'"+TextField1.getText()+"\' AND substr(date,7)||substr(date,4,2)||substr(date,1,2) < substr (\'"+TextField2.getText()+"\',7)||substr (\'"+TextField2.getText()+"\',4,2)||substr (\'"+TextField2.getText()+"\',1,2) AND substr(date,7)||substr(date,4,2)||substr(date,1,2) > substr (\'"+TextField3.getText()+"\',7)||substr (\'"+TextField3.getText()+"\',4,2)||substr (\'"+TextField3.getText()+"\',1,2)");
statementVII.prepare();
statementVII.execute();
statementVII.close();
Anyone sound on this,please guide.Thanks.
you need to use an Or clause together with brackets:
WHERE name='....' AND (yourDateField<yourLowerDate OR yourDateField>yourHigherDate)

Storing Select Query result in Variable Ruby on Rails

I wanted to know how we could store the result of my select query on a variable.
#ppt2 = Ppt.select('slide_name').where('id=?',4)
#ppt1 = Ppt.update_all({:time2=>#ppt2},['id like ?','1'])
Here, slide_name and time2 are both text attributes of the same table ppt.
What happens on the above execution is that the time2 field in id=1 gets the value "#ppt2" whereas I want it to get the value of slide_name from id=4 which does not get stored in #ppt1.
In other words, how do I store the value of the select query in #ppt2 so that it can be used in the next line?
Any help is appreciated.
Call the slide_name method on your first result.
#ppt2 = Ppt.select('slide_name').find(4).slide_name

DISTINCT is not working

SQL query in Ms-Access
INSERT INTO tblTmpEventLog( TrackingNumber, PartNumber, PartNumberChgLvl,
EnteredBy, EventTypeSelected, EventDate )
SELECT DISTINCT tblRevRelLog_Detail.RevRelTrackingNumber,
tblRevRelLog_Detail.PartNumber, tblRevRelLog_Detail.ChangeLevel,
[Forms]![frmEventLog_Input]![EnteredBy] AS EnteredBy,
[Forms]![frmEventLog_Input]![EventTypeSelected] AS EventTypeSelected,
CDate([Forms]![frmEventLog_Input]![EventDate]) AS EventDate
FROM tblRevRelLog_Detail LEFT JOIN tblEventLog
ON (tblEventLog.PartNumber = tblRevRelLog_Detail.PartNumber)
AND (tblEventLog.PartNumberChgLvl = tblRevRelLog_Detail.ChangeLevel)
WHERE ((([tblRevRelLog_Detail]![RevRelTrackingNumber]) =
[Forms]![frmEventLog_Input]![TrackingNumber]))
AND ((tblEventLog.PartNumber) NOT IN
(SELECT tblEventLog.PartNumber FROM tblEventLog
WHERE tblEventLog.EventTypeSelected = 'pn REMOVED From Wrapper'
AND tblEventLog.TrackingNumber =
tblRevRelLog_Detail.RevRelTrackingNumber
AND tblEventLog.PartNumber = tblRevRelLog_Detail.PartNumber
AND tblEventLog.PartNumberChgLvl =
tblRevRelLog_Detail.ChangeLevel
));
DISTINCT keyword for EnteredBy, EventTypeSelected is not working..I mean, data for these columns is not displaying when I use DISTINCT keyword.
EVENTDATE is working fine, but I do not understand why is it not displaying for EneteredBy and EventTypeSelected columns.
Can anyone tell me how to handle this?
It may be that the query can't interpret properly from the form directly as the final data type. However in your date field, you are wrapping it in a function CDATE( ... ). So, the SQL engine knows the result type. I would suggest doing the same for the other fields. Ex: doing a CAST ( ...your form control... as DateTime ) as OtherColumn, etc... I THINK Access allows casting, but not positive. Otherwise, pre-pull the form value into a declared data type variable and use THAT variable in the query AS OtherColumn as you are doing.
Additionally to what #Jack mentioned, you can always go back to your account, look at your historical question, and click on whatever answers actually helped / solve your problems. Some questions never do get answers and that's ok, just give credit to those that DO help.
I have found in the past (I don't remember which old version of Access this was) that if you set the value of a form control in VBA, and then use that control in a query, the query will not see the value you set in VBA. If the user edits the control normally, the query sees the expected value. Perhaps that's what happened here.
To work around that, you can declare a VBA function that returns the desired value. For example, instead of this:
SELECT ..., Forms!MainForm!TextEntry AS TextEntry, ... FROM ...
use this:
SELECT ..., GetTextEntry() AS TextEntry, ... FROM ...
along with this:
Public Function TextEntry() As Variant
TextEntry = Forms!MainForm!TextEntry
End Function