I am trying to get a search query to run. It is as follows. The last two lines of code do not work. It is a check box. If the checkboxYes is checked then pull only the items in the YES/NO field of the database that are checked. If the checkboxNo is checked then pull only the items in the YEs/No field of the database that are checked. If BOTH boxes are checked, then pull all items. I cannot get either scenario to work. All other fields except the check boxes work correctly.
SELECT *
FROM NLog
Where
(([Forms]![FrmSearch]![Combo13] is Null) OR ((NLog.State)=[Forms]![FrmSearch]![Combo13])) and
(([Forms]![FrmSearch]![daterecsrt] is Null) or (((NLog.DateRec) Between [Forms]![FrmSearch]![daterecsrt] And [Forms]![FrmSearch]![daterecend]))) and
(([Forms]![FrmSearch]![datesrttxt3] is Null) or (((NLog.DateRec) Between [Forms]![FrmSearch]![datesrttxt3] And [Forms]![FrmSearch]![dateendtxt3]))) and
(([Forms]![FrmSearch]![checkyes] is Null) or ((NLog.Compchk)=True)) and
(([Forms]![FrmSearch]![checkNo] is Null) or ((NLog.Compchk)=False))
Thank you!
I am using MS Acess 2010. This is using the boxes on a form to run the query.
NLog is a table so why are you referencing fields in the table as [Forms]![FrmSearch]![Combo13] and etc. Those are controls on a form and not fields in your table. Shouldn't your query really be:
SELECT * FROM NLog WHERE
(NLog.State is Null OR NLog.State = Forms!FrmSearch!Combo13)
AND (NLog.DateRec is Null OR NLog.DateRec Between Forms!FrmSearch!daterecsrt And Forms!FrmSearch!daterecend
AND (NLog.DateRec is Null OR NLog.DateRec Between Forms!FrmSearch!datesrttxt3 And Forms!FrmSearch!dateendtxt3)
AND (NLog.Compchk is Null OR NLog.Compchk = True)
AND (NLog.Compchk is Null OR NLog.Compchk = False)
Also, the following will not give you any results:
AND (NLog.Compchk is Null OR NLog.Compchk = True)
AND (NLog.Compchk is Null OR NLog.Compchk = False)
NLog.Compchk can not be both True and False. It is going to be one or the other or Null.
You will need to check if the boxes are checked and build the last part of your query around that, (ie IIF()).
Related
I'm working on a search form based off a single table. I'm working primarily in the query design view. I'd like to have a checkbox on the form so if it is checked (true) it will only return records where the setID is Null. If unchecked (false) it will return all records regardless of if there is a value in setID or not. Looking for a bit of help writing the iif statement (I'm very, very new to this).
source table: Inventory
field: setID
form: frmSearchInventory
form control: ckExcludeSet
iif(Forms!frmSearchInventory!ckExcludeSets = true, Inventory.SetID is Null, Inventory.SetID is not Null)
Close? Also, in the query design view, do I need anything additional in the criteria row? Many thanks!
For a dynamic query, calculate a field that returns SetID or a value in lieu of null: Nz(SetID, "N")
Then criteria under that calculated field:
LIKE IIf(Forms!frmSearchInventory!ckExcludeSets, "N", "*")
An unbound checkbox can be set for triple state. Make sure yours allows only True or False, never Null - set TripleState property to No. Set DefaultValue property to either True or False.
I am trying to create a multi-criteria query based on several combo boxes where if they are left empty, the result should include all records. I have tried several variations of below but to no use.
I did a lot of searching and found to inculde OR 'Control Name' IS NULL but still can't seem to get it working.
I am trying to sum up Revenue grouped by Revenue Identifier (eg. Verified/Unverified/Deleted).
SELECT Sum(sTblLineItemMaster.lineItemTotal) AS SumOflineItemTotal, sTblLineItemMaster.lineItemStatus
FROM sTblActivityMaster INNER JOIN sTblLineItemMaster ON sTblActivityMaster.actID = sTblLineItemMaster.lineItemActID
WHERE
((sTblActivityMaster.actTargetYear=[Forms]![frmActivityList]![cmbSearchYear] Or [Forms]![frmActivityList]![cmbSearchYear] Is Null) AND
(sTblActivityMaster.actTargetMonth=[Forms]![frmActivityList]![cmbSearchMonth] Or [Forms]![frmActivityList]![cmbSearchMonth] Is Null) AND
(sTblActivityMaster.actTargetWeek=[Forms]![frmActivityList]![cmbSearchWeek] Or [Forms]![frmActivityList]![cmbSearchWeek] Is Null) AND
(sTblActivityMaster.actCategory=[Forms]![frmActivityList]![cmbSearchCategory] Or [Forms]![frmActivityList]![cmbSearchCategory] Is Null) AND
(sTblActivityMaster.actCustOwner=[Forms]![frmActivityList]![cmbSearchOwner] Or [Forms]![frmActivityList]![cmbSearchOwner] Is Null) AND
(sTblActivityMaster.actRegion=[Forms]![frmActivityList]![cmbSearchCoordinatorRegion] Or [Forms]![frmActivityList]![cmbSearchCoordinatorRegion] Is Null) AND
(sTblActivityMaster.actMode=[Forms]![frmActivityList]![cmbSearchMode] Or [Forms]![frmActivityList]![cmbSearchMode] Is Null) AND
(sTblActivityMaster.actCreator=[Forms]![frmActivityList]![cmbSearchCoordinator] Or [Forms]![frmActivityList]![cmbSearchCoordinator] Is Null) AND
(sTblActivityMaster.actSection=[Forms]![frmActivityList]![cmbSearchSection] Or [Forms]![frmActivityList]![cmbSearchSection] Is Null) AND
(sTblActivityMaster.actProject=[Forms]![frmActivityList]![cmbSearchProject] Or [Forms]![frmActivityList]![cmbSearchProject] Is Null) AND
(sTblActivityMaster.actRevenueBookStatus=[Forms]![frmActivityList]![cmbSearchRevenue] Or [Forms]![frmActivityList]![cmbSearchRevenue] Is Null) AND
(sTblActivityMaster.actVerificationStatus=[Forms]![frmActivityList]![cmbSearchVerification] Or [Forms]![frmActivityList]![cmbSearchVerification] Is Null) AND
(sTblActivityMaster.actAcceptanceStatus=[Forms]![frmActivityList]![cmbSearchAcceptance] Or [Forms]![frmActivityList]![cmbSearchAcceptance] Is Null) AND
(sTblActivityMaster.actKPIStatus=[Forms]![frmActivityList]![cmbSearchKPI] Or [Forms]![frmActivityList]![cmbSearchKPI] Is Null) AND
(sTblActivityMaster.actInvoicingStatus=[Forms]![frmActivityList]![cmbSearchInvoicing] Or [Forms]![frmActivityList]![cmbSearchInvoicing] Is Null))
GROUP BY sTblLineItemMaster.lineItemStatus;
Can anyone please check if syntax is OK or if any other modification is needed.
Thanks
I like to do it this way:
sTblActivityMaster.actTargetYear=Nz([Forms]![frmActivityList]![cmbSearchYear],sTblActivityMaster.actTargetYear)
That makes it simpler by just having a series of AND without mixing OR and having to deal with the parenthesis mess.
As I am using ComboBoxes to get the search criteria and the empty combo boxes return a "" and not NULl, so the query wasn't returning the results.
So what I did was that when search button is pressed to run the query, I set all empty or "" comboboxes to NULL in VBA. That did the trick and results appeared.
I have a model with this field:
carousel_image = models.ImageField(upload_to='news/%Y/%m/%d, null=True, blank=True)
I was wondering why exclude(carousel_image__isnull=True) and other queries that check if field is or isn't null aren't working.
I checked my sqlite3 db and received this:
sqlite> select carousel_image from asgeos_site_news;
news/2020/12/23/index.jpeg
news/2020/12/23/gradient-1.jpeg
( 3 blank lines )
I also tried adding WHERE carousel_image = NULL and it returned nothing.
Why my images are not null? They're just a blank lines. I have to use carousel_image__exact='' to exclude them right now.
File fields can't be set to null in the db, here's the reference from django documentation
blank = True
is enough.
blank is stored as ''
Tired brain - perhaps you can help.
My table has two bit fields:
1) TestedByPCL and
2) TestedBySPC.
Both may = 1.
The user interface has two corresponding check boxes. In the code I convert the checks to int.
int TestedBySPC = SearchSPC ? 1 : 0;
int TestedByPCL = SearchPCL ? 1 : 0;
My WHERE clause looks something like this:
WHERE TestedByPCL = {TestedByPCL.ToString()} AND TestedBySPC = {TestedBySPC.ToString()}
The problem is when only one checkbox is selected I want to return rows having the corresponding field set to 1 or both fields set to 1.
Now when both fields are set to 1 my WHERE clause requires both check boxes to be checked instead of only one.
So, if one checkbox is ticked return records with with that field = 1 , regardless of whether the other field = 1.
Second attempt (I think I've got it now):
WHERE ((TestedByPCL = {chkTestedByPCL.IsChecked} AND TestedBySPC = {chkTestedBySPC.IsChecked})
OR
(TestedByPCL = 1 AND TestedBySPC = 1 AND 1 IN ({chkTestedByPCL.IsChecked}, {chkTestedBySPC.IsChecked})))
Misunderstood the question.
Change the AND to an OR:
WHERE TestedByPCL = {chkTestedByPCL.IsChecked} OR TestedBySPC = {chkTestedBySPC.IsChecked}
Also:
SQL Server does not have a Boolean data type, it's closest option is a bit data type.
The usage of curly brackets suggests using string concatenations to build your where clause. This might not be a big deal when you're handling checkboxes but it's a security risk when handling free text input as it's an open door for SQL injection attacks. Better use parameters whenever you can.
it's working if all the feilds is entered by user. i need a code that combine all the sql command. user may enter in the one field or two field or all the three fields. i need to search the database with one field query or two feild query or three feilds query.
i have try it with where help is a table & search-test is form & contract no,username & date of feild in database and forms.
where the help(table).cont_no(field) is equal or not equal to search-test(form name).cont_no(text box field)
SELECT *
FROM help
WHERE ( forms ! [search-test] ! cont_no = ''
OR help.cont_no = forms ! [search-test] ! cont_no )
AND ( forms ! [search-test] ! username = ''
OR help.username = forms ! [search-test] ! username )
AND ( forms ! [search-test] ! cbo_date = ''
OR help.DATE = forms ! [search-test] ! cbo_date );
I think what you mean is that you want a record included in the result set if the record fields contain a match to the parameters supplied. If a specific parameter is not supplied, then the corresponding field should always be considered a positive match.
That is, if no parameters were supplied, the entire table should be returned.
As for a solution, just a guess (I'm not an MS Access expert, is this for MS Access?), but can you use the iif function to force a match instead? Again, I'm not sure about the syntax, but the underlying logic should work.
Change your WHERE clause to
WHERE
(IIF(forms![search-test]!username is not null, forms![search-test]!username, help.cont_no) = help.cont_no
AND
(IIF(forms![search-test]!cont_no is not null, forms![search-test]!cont_no, help.username) = help.username
AND
(IIF(forms![search-test]!cbo_date is not null, forms![search-test]!cbo_date, help.dbo_date) = help.dbo_date