iif statement in a query used for criteria (1 Viewer)

AccessLuke

New member
Local time
Today, 07:35
Joined
May 29, 2014
Messages
2
Could somebody help me please?

I have a database for case files, with a search form containing various text boxes that lets you enter criteria for the search so that when you click the search button, the results are displayed in a report. However I would like to add a tick box on the search form, so that when ticked it allows cases of all statuses (i.e. include case files that have their “Status” field set to the value “Closed”) and when not ticked I want it to include all records that are not “Closed”.

My Form is called “SearchForm”
My tick box is named “IncludeClosed”.
[Status] is a text field in my Table that has values such as “Open”, “Pending” and “Closed”.

I have used the following code:

IIf([Forms]![SearchForm]![IncludeClosed]=-1,[Status],[Status]<>"Closed")


Where am I going wrong?

Many thanks and kind regards

Luke
 

CJ_London

Super Moderator
Staff member
Local time
Today, 15:35
Joined
Feb 19, 2013
Messages
16,611
unfortunately you can't create your criteria like that it needs to be like this:

Code:
WHERE ([Forms]![SearchForm]![IncludeClosed]=-1 AND Status="closed") OR ([Forms]![SearchForm]![IncludeClosed]=0 AND [Status]<>"Closed")
 

BlueIshDan

&#9760;
Local time
Today, 11:35
Joined
May 15, 2014
Messages
1,122
Show me your full query in code brackets please.
 

AccessLuke

New member
Local time
Today, 07:35
Joined
May 29, 2014
Messages
2
Thank you both for your help

My query in SQL View looks like this (I hope this is what you meant):


SELECT Main.PermitNumber, Main.PERM, Main.PermitType, Main.Status, Main.DateOfEnquiry, Main.DateFormsSent, Main.CompanyName, Main.CompanyBuildingNameOrNumber, Main.CompanyStreet, Main.CompanyTown, Main.CompanyCounty, Main.CompanyPostCode, Main.ContactName, Main.ContactNumber, Main.ContactEmail, Main.SiteNameOrNumber, Main.SiteStreet, Main.SiteTown, Main.SitePostCode, Main.DateFormsRecieved, Main.DateDepositPaid, Main.DateFeePaid, Main.DepositValue, Main.FeeValue, Main.DateInsuranceExpiry, Main.DateDepositReturned, Main.Vehicle1Registration, Main.Vehicle1Weight, Main.DatePermitApproved, Main.Scanned, Main.OverOrUnderOneAndHalf, Main.Comments, Main.Reason, Main.PhotosOfSiteAfterWorkComplete, Main.NNoticeReceived, Main.LevelAInspectionDateCompleted, Main.RNoticeDateCompleted, Main.LevelBInspectionDateCompleted, Main.GauranteeExpiryDate, Main.[LetterSentConfirmationCompletionOfWorks-Date], Main.[LetterSentConfirmingCompletionOfWorks-Date], Main.LevelCInspectionDueDate, Main.LevelCInspectionCompletedDate, Main.[LetterSentConfirmingGauranteePeriodHasExpired-Date], Main.Flag, Main.ActionPoints
FROM Main
WHERE (((Main.Status)=IIf([Forms]![SearchForm]![IncludeClosed]=-1,[Status],[Status]<>"Closed")) AND ((Main.CompanyName) Like "*" & [Forms]![SearchForm]![Cname] & "*") AND ((Main.CompanyBuildingNameOrNumber) Like "*" & [Forms]![SearchForm]![Cnum] & "*") AND ((Main.CompanyStreet) Like "*" & [Forms]![SearchForm]![Cstreet] & "*") AND ((Main.CompanyTown) Like "*" & [Forms]![SearchForm]![Ctown] & "*") AND ((Main.CompanyCounty) Like "*" & [Forms]![SearchForm]![Ccounty] & "*") AND ((Main.CompanyPostCode) Like "*" & [Forms]![Cpostcode] & "*"));


I tried taking out the text in red and replacing it with this blue code code CJ_London kindly posted:

WHERE ([Forms]![SearchForm]![IncludeClosed]=-1 AND Status="closed") OR ([Forms]![SearchForm]![IncludeClosed]=0 AND [Status]<>"Closed")

But when I did this and clicked save I was met with an error message saying "Extra ) in query expression"

The search part of my query works fine and when my tick box is ticked, it allows 'Closed Permits' to be included in the search, but when it is not ticked I get 0 results.

Thank you for your help so far, it is much appreciated.
 
Last edited:

BlueIshDan

&#9760;
Local time
Today, 11:35
Joined
May 15, 2014
Messages
1,122
You should try going over your brackets and letting me know how you are making out from there.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 15:35
Joined
Feb 19, 2013
Messages
16,611
Show me your full query in code brackets please.

(I hope this is what you meant):
Almost - correct for the full query but not in code brackets - these can be found in the advanced editor on the # button

you didn't explain that there are other elements in the criteria. I suspect you need an extra two brackets just after the WHERE and one at the end

Code:
WHERE ((([Forms]![SearchForm]![IncludeClosed]=-1 AND Status="closed") OR ([Forms]![SearchForm]![IncludeClosed]=0 AND [Status]<>"Closed"))
Alternatively remove all the other brackets - they are not required. The ones I provided are required to separate 'ands' and 'ors' to maintain the logic
 

BlueIshDan

&#9760;
Local time
Today, 11:35
Joined
May 15, 2014
Messages
1,122
Almost - correct for the full query but not in code brackets - these can be found in the advanced editor on the # button

you didn't explain that there are other elements in the criteria. I suspect you need an extra two brackets just after the WHERE and one at the end

Code:
WHERE ((([Forms]![SearchForm]![IncludeClosed]=-1 AND Status="closed") OR ([Forms]![SearchForm]![IncludeClosed]=0 AND [Status]<>"Closed"))
Alternatively remove all the other brackets - they are not required. The ones I provided are required to separate 'ands' and 'ors' to maintain the logic

I need to learn to explain things like this.
The same things I have said, just 1000x better :(
Thanks thought :)
 

Users who are viewing this thread

Top Bottom