mesba713
mesba713

Reputation: 11

Access Query Form with multiple Checkboxes

i have a newbie question and would appreciate every help. I have a form that runs a query. The query is based on one single table. In this table there are several Projects.(Project 1, Project2,Project3....etc)

In the form there are several checkboxes, where the user can choose which projects he wants to chek. the thing is with my code if i check 2 projects he shows only the records where both projects are selected in the table. But the I want it to show all the records where either of them is shown. Ex. If i chech the boxes for Project1 and the checkbox for Project2 it should show the records where Project 1 is checked and also the records where Project 2 are also checked.

SELECT Vergabeumfang.S63T4, *
FROM Vergabeumfang
WHERE (    
        ((Vergabeumfang.VSS_LAW) Like "*" & [Forms]![Vergabeumfang]![VSS_LAW_Box] & "*" 
            Or (Vergabeumfang.VSS_LAW) Is Null
        )     
        And ((IIf([Forms]![Vergabeumfang]![S63T4_box]=-1,([Vergabeumfang].[S63T4])=True,([Vergabeumfang].[S63T4])=False 
                or ((Vergabeumfang.[S63T4])=True )))
        )                    
        And ((IIf([Forms]![Vergabeumfang]![S63T2_box]=-1,([Vergabeumfang].[S63T2])=True,([Vergabeumfang].[S63T2])=False 
            or ((Vergabeumfang.[S63T2])=True )))
        )
        AND ((Vergabeumfang.V_Commodity_Name) Like "*" & [Forms]![Vergabeumfang]![Commodity_Name_Box] & "*" 
            Or (Vergabeumfang.V_Commodity_Name) Is Null
        ) 

Upvotes: 1

Views: 5942

Answers (1)

James Toomey
James Toomey

Reputation: 6082

The following query should work for you, assuming I'm understanding right, with a couple of caveats I'll mention at the end. Also, no need to select the field S63T4 specifically at the beginning since the * will retrieve it:

SELECT *
FROM Vergabeumfang
WHERE 
(
    Vergabeumfang.VSS_LAW Like "*" & [Forms]![Vergabeumfang]![VSS_LAW_Box] & "*" Or Vergabeumfang.VSS_LAW Is Null
)
AND 
(
    Vergabeumfang.V_Commodity_Name Like "*" & [Forms]![Vergabeumfang]![Commodity_Name_Box] & "*" Or Vergabeumfang.V_Commodity_Name Is Null
)
AND 
(
    (
        Vergabeumfang.S63T2 = iif([Forms]![Vergabeumfang]![S63T2_box]=-1, True, -999)
    )
    OR
    (
        Vergabeumfang.S63T4 = iif([Forms]![Vergabeumfang]![S63T4_box]=-1, True, -999)
    )
)

Access will reformat the query once you paste it in, but these indents make the logic more clear. The -999 is a dummy value which will never be true on a Yes/No field, to ensure that it always returns false if the criteria checkbox is unchecked. I reconstructed your table based on the info given, does this look about right? Nonsense data added to the text fields for experimentation's sake:

enter image description here

When the form first opens, it won't show any data, like this: enter image description here

When you put a checkmark for S63T4 and hit Requery (you'll need some way to tell Access to requery, since it won't do automatically--the button is the easiest way, with Me.Requery for the button's click event, but you could also add this to the AfterUpdate events for the criteria checkboxes/textboxes too), it will look like this:

enter image description here

It looks like you want to further filter on partial text entries, so this picture shows the further filtering when the letter "t" is typed in along with nulls in that field:

enter image description here

Here's a pic filtering on the 2nd checkbox:

enter image description here

Now, when you select both checkboxes, I wasn't sure if you wanted to show projects that are

a) either S63T2 or S63T4

or

b) only S63T2 and S63T4

It sounded to me like you wanted to be able to show projects that are either type, which is what this picture shows:

enter image description here

Now, a couple of tips:

  1. It's easier to construct the SQL statement in code and then apply it to the Form's Recordsource property, especially when you start adding more criteria options. Trying to control all of this with the limited IIF statements is going to get completely unmanageable after a point, because you start having to nest them.
  2. If a project can have multiple types (I'm assuming "S63T4" and "S63T4" are project types), and might acquire more in the future, so that a project might have 10 types someday, it'll be much more flexible to create a new table of project types linked on the ID key, like this:

enter image description here

The criteria form can now look like this:

enter image description here

And now your form doesn't need to be edited when you add a new project type; it'll just appear automatically in the list box.

Anyway, good luck with the project!

Upvotes: 2

Related Questions