Multiple Checkboxes

rayape

Registered User.
Local time
Today, 12:51
Joined
Dec 12, 2012
Messages
56
Hello,

I need to run a query based on selection made using check boxes. The code I am using is here:

For the Combobox:

IIf([Forms]![Expenses]![Check17]=-1,[Forms]![Expenses]![Combo13],"")

IIf([Forms]![Expenses]![Check15]=-1,[Forms]![Expenses]![Combo10],"")

The query does not run when either of the check boxes is selected. The query returns a value only when both the check boxes are selected.

Please help me with the correct way forward.

Thank you.
 
Can you show us the whole query? Do you get an error message?
 
Hi jdraw,

Thanks for replying.

The whole query is what I've posted in the previous post. The SQL version of the query is here. I hope it helps. I tried changing the AND to OR in the SQL query. It did not work the way I wanted it to. I don't get any error messages. The query returns no value.

SELECT Expenses.[Date Purchased], Expenses.Store, Expenses.Item, Expenses.Category, Expenses.Price
FROM Expenses
GROUP BY Expenses.[Date Purchased], Expenses.Store, Expenses.Item, Expenses.Category, Expenses.Price
HAVING (((Expenses.Store)=IIf([Forms]![Expenses]![Check17]=-1,[Forms]![Expenses]![Combo13],"")) AND ((Expenses.Category)=IIf([Forms]![Expenses]![Check15]=-1,[Forms]![Expenses]![Combo10],"")));
 
jdraw is right, the hole query would be very useful.

I tried to figure out your problem and I have to ask: it's really necessary to evaluate both, the checkboxes and the comboboxes?

However, I developed - I hope - a solution for the problem. You'll find it in attached accdb file.

I apologise the not really logic data you'll find in the comboboxes. They're only for illustration purposes. - :o

StarGrabber
 

Attachments

Thank you, StarGrabber! It looks like what you have posted is what I was looking for.

Please help me understand two questions:

What was I doing wrong in my query?
How did you build your query (the logic)? (Also, I can't directly enter [crit1] or [crit2] in the field. How did you do that?)

Forgive my ignorance, I'm super new to access. :(

Thank you.
 
To say it frankly, I'm not sure what you did wrong. I started with creating a table to have some data to query. Then I build the form in order to have the control names corresponding to your case. After this I created a simple query (using the access functionality by pressing the corresponding button in the ribbon) and started testing right away. There were some error messages so I wasn't able to run the query too.
Access beefed something like "expression in criteria statement to complex. Unable to evaluate...". After I had removed your quotation marks at the end of your IIf-statements (that is the 'false'-part) there was a slight improvement. -

Before wasting to much time I replaced your IIf-statements by "[crit1]" and "[crit2]" what is quite simple at the query side, but much more costly on the code side.

What do you mean by saying "I can't directly enter [crit1] or [crit2] in the field"? When you open the query (with double click on the query name) Access first asks for '[crit1]' and '[crit2]'. At this point you can do what you want, you can enter a value or leave the input box field empty. But the result (the records found) will be accordingly.

The SQL string in the code ('cmd.CommandText') was copied from the query itself, which you can disply in SQL view mode (see first button on left side of the query tools ribbon).

A few minutes before I posted you answered giving us the hole query. I will try it when I will be back from holiday.

Don't bother, you are not ignorant! Access is easy to learn indeed - but it's a bit tricky sometimes.

Maybe there is a simpler way to do it. I can imagine other experts (e.g. jdraw) will help us.

StarGrabber :)
 
StarGrabber, thank you very much for the explanation. It is definitely costly on the code side (as you pointed out). For somebody that does not have any background in computer science/programming, I'm going to have a very hard time writing or even understanding the code which you have done so wonderfully.

Thank you again and really appreciate if you could tell me my mistake.

Happy Holidays!
 
Hi rayape,

before this year is over let's go the whole hog.

I think I got it. There are two reasons for the undesired/missing function of your query: 1. the operator 'AND' and 2. the value of the false part of your IIf-statements.

If a checkbox isn't checked, the statement returns the value "", i.e. an empty string. But this value obviously cannot be evaluated by Access-SQL. So either you hand over a parameter or you don't. But you cannot hand over an empty one (do not confound with 'null').

I've asked you if you need the checkboxes at all costs. Because without them you can get the same functionality with less effort. I changed your query to a similar one to mine posted last time. This changed query ('qryExpenses') has no IIf-statements anymore. It evaluates only the two comboboxes. Easy to read, easy to understand, easy to handle!

From a logic point of view it should also be possible to return all the records of the table. We can achieve this by leaving both comboboxes empty and by adding another "Or" line in the query handling the case that both parameters are null.

If you open the form 'Expenses' and press the 'Run Query' button you will see all records of the table shown in the query. Then you may select a value in one or both combobox(es) and press 'Run Query' again (without having closed the query window before. VBA code takes care of this).

Besides... the strange error message I saw the last time was caused by a data type incompatibility. As you noticed, one of the criteria columns in 'tblDemo' has a numeric data type which does not go with the false-part of the IIf-statement. -

Little I know about the data structure of your application but if you save 'Stores' and 'Categories' as text values you will have to change the whole column of your table in case the term should be renamed or in case of a spelling change. I recommend you to use separate tables ('tblCategories', 'tblStores') and replace the values by the corresponding numeric values ('ID'). After this you should change the field data type of the two fields in 'Expenses' and create some relationships between the tables.

One more thing: if I say "Expenses", do you know what I mean? Maybe. Maybe, because you have a table and a form in your application with the same name. - And if I tell you something about 'Inventory' (e.g. in my database), do you know what is meant? No, you don't. 'Inventory' can be a table, a query, a form, a textbox, a listbox, a recordset or whatever. Therefore Access developers normally use prefixes, like 'txt' for a textbox, 'tbl' for a table, "chk" for a checkbox and so on. Well known is the so called 'Reddick VBA Naming Convention' (search the web for further details). But you don't have to use this one. You can create your own convention if you want to. Important is only you use one at all. Your application will be so big and complex some day that you will get lost reading code without a naming system. Or maybe it will not grow but you start developing another application and half a year later you open the first database again. Would you remember everything? - And last but not least, if you share code with others - like you did here in this forum - using prefixes it's a lot easier for them to understand. Sorry, but control names like 'Check17' or 'Combo13' are really bad. This time it was me who had "a very hard time understanding the code". ;) Object names in general and control names in particular should be descriptive, like 'tblExpenses', 'qryExpenses', 'cboStores', 'chkIncludeCategory' and so on.

Have a good start into a new Access year!

StarGrabber
 

Attachments

StarGrabber, firstly sorry for replying so late. I was moving so did not get a chance to sit in front of the computer.

I did what you have suggested and it worked perfectly. I can't thank you enough.

And regarding the checkbox, it's not a must have but would have been nicer to have the user make the selection in order to activate the combobox. But I can live without it. Not a problem.

I have noted your second suggestion about having uniquely identifiable names for queries, forms, text boxes, and so on. It's true I was not paying attention to that. From now on, the same mistake will not continue.

Again, thank you very much.
 

Users who are viewing this thread

Back
Top Bottom