473,387 Members | 1,561 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Search form with multiple tick boxes wont ignore unticked boxes

Hi,

I'm basing a query on the variables used in a form. The database contains lots of problems, say with a computer and a tick box for the apropriate component like monitor, keyboard, mouse etc. and each has several eg problem 1 has a monitor and mouse problem

My form then has what to look for tick boxes (tri-state) for each of these. I want my query to include all monitor problems when that is ticked and all monitor and mouse problems when both ticked, no monitor problems when unticked and ignore a parameter when the tick box is null (greyed out)

I have come up with:
Expand|Select|Wrap|Line Numbers
  1. WHERE ((((Table1.[Monitor])=Forms![Query Form]![Monitor tick]) Or Is Null) And (((Table1.[Keyboard])=Forms![Query Form]!Keyboard tick) Or Null) And (((Table1.Mouse)=Forms![Query Form]!Mouse tick) Or Null)
but I cant get it to work, it either gives my all the entries in the database or sometimes a parameter query box with "Forms![Query Form]![Monitor tick]"

any help or advice would be great, thanks
Apr 30 '08 #1
18 3072
Stewart Ross
2,545 Expert Mod 2GB
Hi. What data type are you using for the fields bound to your tick boxes? If they are Boolean (Yes/No) types an Access quirk is that there is no null state for Boolean values, so any test for Is Null on a boolean will fail. Booleans will evaluate as False when not set. You need to rethink the logic of what you wish to achieve...

On the other issue, Access sometimes has difficulties in interpreting direct references to form fields in queries, treating the form field as a parameter. If this becomes a problem for you it is possible to resolve it by replacing the form field references with a custom function which will return the field value from the form for you. the Jet database engine has no problems with function calls, but can fail in mysterious ways when faced with form field references.

If you need to use it, the function below should be placed in any public code module or a new module if you do not have any existing modules.
Expand|Select|Wrap|Line Numbers
  1. Public Function FormFieldValue(FormName As String, FieldName As String)
  2.     FormFieldValue = Forms(FormName).Controls(FieldName)
  3. End Function
In place of the direct form field references in your query criteria you then use calls to the function as follows:
Expand|Select|Wrap|Line Numbers
  1. Formfieldvalue("Query Form", "Monitor Tick")
and so on.

Good luck with resolving the problems.

-Stewart
Apr 30 '08 #2
Thanks for the advice on using form fields as query values, that was something that I refused to belive wouldn't work but I guess sometimes even in programmes like Access there can be bugs like that!

Anyway, I didn't give any thought to the data type of the fields. I just inserted check boxes, named them appropriately and in the properties of the check box I changed the vale 'triple state' to yes, assuming this gave me a variable that had three staes, Yes, No and Is Null. Was I wrong to also assume this?

My workaround which will result in a bit of a clunky for is to create a drop down for each problem with the values Yes, No and Is Null (displayed as Ignore) and use this parameter for my query but I am not sure this will work either. If the SQL search through the database uses these values will it do what I want as I had written it?
May 1 '08 #3
Stewart Ross
2,545 Expert Mod 2GB
Hi. What matters most is what the field types of the fields in Table1 are. Your SQL WHERE in post 1 clearly shows that you are testing your form tickboxes against fields such as Table1.[Mouse] and Table1.[Monitor]. If these are boolean then the tri-state form boxes will not help you because the fields in the table can't represent the null state. Integer fields can, however, as null is an allowed state for a Long or Integer field. You would need to make sure that there is no value set in the Default Value property, however.

I would suggest that you revise the field types to Integer for the tick fields and do some tests with your form at table and query level to see what happens when you add test records to the table without setting these fields. In particular, check for null values actually being recorded. I would try to avoid using drop-downs with things like Null on them as text, because it won't mean much for users.

-Stewart
May 1 '08 #4
OK, thanks, I'm smacking my head in disgust!!! I wrote the SQL ages ago and came back to it recently and realised I had been asking it to do something stupid by basically saying where field 'x' (which is a yes or no) is equal to null

what I thought i was saying was include this field if the tick box is equal to null, i'll have another look at it
May 1 '08 #5
So with the new Public Function:

Expand|Select|Wrap|Line Numbers
  1. WHERE (IF Formfieldvalue("Query Form", "Monitor tick") <> IS Null THEN (Table1.[Monitor])=Formfieldvalue("Query Form", "Monitor tick"));
  2.  
Not being a big SQL user am I right in using logic here inside a WHERE statement or am I again going down the wrong track!
May 1 '08 #6
Stewart Ross
2,545 Expert Mod 2GB
Ahh, not quite there with the SQL yet. If what you want to do is to check whether the form field matches the tick box if it is not null, and include it anyway if the tick box is null, there is a simpler way using the Nz function to return True (-1) if the form field is null:

Expand|Select|Wrap|Line Numbers
  1. WHERE (Table1.Monitor = CBool(Nz(Formfieldvalue("Query Form", "Monitor tick"), -1))) AND (Table1.Mouse = ...
The CBool is a convert to boolean function which will ensure that the value is correctly interpreted as a true/false value (as Nz is likely to return a string).

-Stewart
May 1 '08 #7
NeoPa
32,556 Expert Mod 16PB
My main rcecommendation would be to create the SQL on the fly in your form code. This is much more straightforward. However, if this is not possible or practicable for any reason, please read on.

For this you will need to change the FormFieldValue() procedure slightly to return a Variant value. Otherwise Null values will cause a crash. This also assumes that, whatever the type of the fields in your table, they only ever contain TRUE (all 1 bits = -1) or FALSE (all 0 bits = 0) values.
Expand|Select|Wrap|Line Numbers
  1. WHERE (((FormFieldValue("Query Form", "Monitor tick") IS NULL)
  2.    OR (FormFieldValue("Query Form", "Monitor tick") = Table1.Monitor))
  3.   AND ((FormFieldValue("Query Form", "Mouse tick") IS NULL)
  4.    OR (FormFieldValue("Query Form", "Mouse tick") = Table1.Mouse))
  5.   AND (...))
Clearly, this can end up as quite a convoluted set of SQL, but it should nevertheless work.
May 1 '08 #8
NeoPa
32,556 Expert Mod 16PB
My experience of accessing form controls from SQL is probably not as extensive as many, so take what I say with caution. I haven't experienced any problems referencing controls myself. Here is a link on how it should be done in case it's any help (Referring to Items on a Sub-Form). It covers more than simple subform referencing btw.

Hope this helps (& I'm not trying to tread on toes here Stewart). Appologies if it seems that way.
May 1 '08 #9
I hate asking more because you have provided me with so much help today so if you want to leave this to someone else you can!

I have two problems:

1:
despite using the function as you gave me exactly in Access Class Objects, I get the following error message:

Undefined function <name> in expression. (Error 3085)
You entered an SQL expression that includes a Function procedure name that cannot be recognized. Make sure the function exists, that it can be used in SQL expressions, or check the expression to make sure you entered the name correctly.

This is a small ish problem and at the moment I am not getting the annoying parameter pop-up box that I sometimes get so its not essential to sort out

2:
The expersion in the last post would be perfect for treating the Null state of the check box as 'Yes' but I want the SQL query to ignore this if it is Null (including all entries in the table that have either 'Yes' or 'No') instead of treating it as a 'Yes'

I have about 20 check boxes (its a database of problems with our automation systems to be looked up to diagnose the root cause of future issues) and it is neccessary when looking up previous problems to have a blank form with all these check boxes in Null state. and a button to run the Query

For example, to look up all previous issues with 'conveyor B' and 'feeder B' but not the 'controler module', I want to check conveyor B box, check feeder B box and change the 'controler module' box to unticked. And all the other 17 boxes will be left in their null state so the query will return instances where these fields are both 'yes' and 'no' in other words ignoring them when the are Null



I thought it would be a minor issue to get over but i've been stuck on it for so long now its killing me!!!
May 1 '08 #10
wow it took me so long to write that and other distractions there are two more replys, it looks promissing and I will give it a try, thanks
May 1 '08 #11
Tried that and its great to make some progress but its a killed when it all falls apart!!! (excuse me for using the actual code and not the simpler examples i had been using)


Expand|Select|Wrap|Line Numbers
  1. WHERE (((Forms![Query Form]![IMP/IMPexquery] Is Null)
  2.  Or (Forms![Query Form]![IMP/IMPexquery]= Table1.[IMP/IMPex])))
worked great, picked only yes when ticked, only no when unticked and both when null so i tried to add an and statement to it:

Expand|Select|Wrap|Line Numbers
  1. WHERE (((Forms![Query Form]![IMP/IMPexquery] Is Null)
  2.  Or (Forms![Query Form]![IMP/IMPexquery]= Table1.[IMP/IMPex]))
  3. AND ((Forms![Query Form]![LSM/OCR] Is Null)
  4.  Or (Forms![Query Form]![LSM/OCR]=Table1.[LSM/OCR]))
  5.  
but it completely ignores the second part!! it does the exact same for the first bit and doesnt take the second bit into account
May 1 '08 #12
I think i found the problem, when i looked at the SQL in design view it showed this:



it doesent seem to be linking the parameters properly, instead it creates new ones that dont work as proper AND statements!!

oh well, it looked so nice in SQL
May 1 '08 #13
Stewart Ross
2,545 Expert Mod 2GB
Hi NeoPa. The problems with form field references typically arise in queries which do extensive grouping and totalling, rather than those involving query criteria in general. It becomes a certainty that a failure will arise if such a query is converted to a crosstab - Jet generates a failure saying that it does not recognise the query field name as a valid name.

For this particular application the use of the indirect function is probably not necessary - but in itself it should not cause any issues, any more than the use of any other function in a query expression would do.

I take no offence at your input at all - I really appreciate your very constructive input and experience on this and other topics you answer and contribute to.

Reading the last posts in this thread there is more to do to resolve the issues arising, and I will check back later when I have more time to look at these in detail.

Cheers

Stewart

My experience of accessing form controls from SQL is probably not as extensive as many, so take what I say with caution. I haven't experienced any problems referencing controls myself. Here is a link on how it should be done in case it's any help (Referring to Items on a Sub-Form). It covers more than simple subform referencing btw.

Hope this helps (& I'm not trying to tread on toes here Stewart). Appologies if it seems that way.
May 1 '08 #14
NeoPa
32,556 Expert Mod 16PB
...
worked great, picked only yes when ticked, only no when unticked and both when null so i tried to add an and statement to it:
Expand|Select|Wrap|Line Numbers
  1. WHERE (((Forms![Query Form]![IMP/IMPexquery] Is Null)
  2.  Or (Forms![Query Form]![IMP/IMPexquery]= Table1.[IMP/IMPex]))
  3. AND ((Forms![Query Form]![LSM/OCR] Is Null)
  4.  Or (Forms![Query Form]![LSM/OCR]=Table1.[LSM/OCR]))
but it completely ignores the second part!! it does the exact same for the first bit and doesnt take the second bit into account
There's a lot in here so I'll try to get to all eventually.

Your WHERE clause looks fine except that it's missing a final closing parenthesis. I don't know if this is Copy/Pasted (it should be) as I would expect Access to show a message in this situatioon rather than allowing the query to run in that state.
May 1 '08 #15
NeoPa
32,556 Expert Mod 16PB
I think i found the problem, ...
it doesent seem to be linking the parameters properly, instead it creates new ones that dont work as proper AND statements!!

oh well, it looked so nice in SQL
Actually, that is displaying it perfectly (and looks fine as far as I can see).
May 1 '08 #16
NeoPa
32,556 Expert Mod 16PB
...
I take no offence at your input at all - I really appreciate your very constructive input and experience on this and other topics you answer and contribute to.
...
@Stewart - I'm pleased to hear :)
I'll have to defer to you on the form reference business. I tend not to play there very much (Almost never use Cross-Tabs).

@Sweeneye - To help us better understand your current position, can you post a sanitised version of your SQL. This would include tests for only two of the CheckBoxes and would be tested so you know exactly how it behaves. Please make sure to Cut/Paste the SQL across to avoid any possibility of errors.
May 1 '08 #17
This little bit of programming has caused me more stress!!! I finally found out my problem and its one of those stupid little things, the code should have looked like this:

Expand|Select|Wrap|Line Numbers
  1. SELECT Table1.[IMP/IMPex], Table1.[LSM/OCR]
  2.  
  3. FROM Table1
  4.  
  5. WHERE ((Forms![Query Form]![IMP/IMPexquery] Is Null) 
  6. Or (Forms![Query Form]![IMP/IMPexquery]=Table1.[IMP/IMPex])) 
  7. And ((Forms![Query Form]!LSMquery Is Null) 
  8. Or (Forms![Query Form]!LSMquery=Table1.[LSM/OCR]));
If you cant spot the difference in the WHERE statement it's where I had been naming things slightly incorrectly and thats the only problem!!!

I could have gone on for hours with that, thanks for the help all, its been invaluable!!
May 2 '08 #18
NeoPa
32,556 Expert Mod 16PB
No worries.

Sometimes, just the exercise of putting the question together enables you to see things you missed earlier.

Congratulations on solving the problem :)
May 2 '08 #19

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Craig M | last post by:
Hi, I have a form, frmInvoices and a subform, frmInvoicesSub. On the parent form, i have a "print report" button, that prints a report depending on an ID on the parent form. Each record in...
4
by: Mason | last post by:
This is probably an incredibly newbie-ish question. I just haven't had the cause to use many subforms before, so I'm pretty sure I just don't understand it correctly (even after reading up on it)....
1
by: Frustrated Developer via DotNetMonster.com | last post by:
I have developed a form that would allow the user to load and search a database several ways, by data range using two combo boxes, by specific number entered in a text box or all database entries....
0
by: Toby Inkster | last post by:
Like me, you are probably confronted with many requests for surveys, questionnaires, feedback forms, registration forms and so forth: forms where the processing requirements are very simple (store...
0
by: Simon | last post by:
Dear reader, Under Tools>>Options>>Tables/Queries there is a setting possible of "SQL Server Compatible Syntax (ANSI 92)". The two tick boxes here are · "This database" · ...
2
by: Homey! | last post by:
Hello all I am new to Access. I have imported data from an old FoxPro 2.x database. This is probably the most basic function but I cant get a search box to work. I need to search for company name...
9
by: thebison | last post by:
Hi all, I hope someone can help with this relatively simple problem. I am building a timesheet application using ASP.NET C# with Visual Studio 2003.As it is only a protoype application, my...
14
by: jacc14 | last post by:
Hi there. I am sure there is an easy solution to this and not sure why mine doesnt work? In the main form I have a "closed"tick box. In the sub form I also have several related records each...
6
by: woodey2002 | last post by:
Hi Everyone. Thanks for your time. I am trying to create a search form that will allow users to select criteria from multiple multi select boxes. So far i have managed to achieve a search option...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.