Solved

Multi field criteria entry form

Posted on 2014-01-23
3
628 Views
Last Modified: 2014-01-29
I have a form which contains 5 unbound fields.

The form is meant to be a form the user can use to select the criteria for a query.

For example, one of the fields is "Bay".  In the query then I have this in the criteria:

Like "*" & [Forms]![frmSelectCriteriaForInventoryExports]![txtBay] & "*"

So the user can enter a value in this field and the query will return the records that have that value for "Bay".

But I also have a two fields for dates.  They are txtStartDate and txtEndDate.

In the query criteria I have:

Like "*" & Between Forms![frmSelectCriteriaForInventoryExports]![txtStartDate] and Forms![frmSelectCriteriaForInventoryExports]![txtEndDate] & "*"

But that doesn't work.

???
0
Comment
Question by:SteveL13
  • 2
3 Comments
 
LVL 36

Expert Comment

by:PatHartman
ID: 39805035
It won't work because dates are not stored as strings and LIKE only works with strings.  I also wouldn't use LIKE at all unless I have partial strings since LIKE can prevent the query engine from optimizing the query and force it to read EVERY record, one at a time.  If you have a partial string, you have to take the hit but if you are working with combos - don't use LIKE since you never have a partial string.  Here's what I do with forms like this:

Where (fld1 = Forms!yourform!fld1 OR Forms!yourForm!fld1 Is Null)
AND (fld2 = Forms!yourform!fld2 OR Forms!yourform!fld2 Is Null)
AND (date1 Between Forms!yourForm!fromdate AND Forms!yourform!thrudate OR Forms!yourForm!fromdate Is Null)
AND ...

Notice the parentheses.  They are critical when your expressions are complex and include both AND and OR.  On your form, you can edit the from and thru dates to make sure both exist when one does so the query only needs to check one.

Access is going to destroy all your wonderful formatting (plus add monumental numbers of extraneous parentheses) so it can render the criteria in QBE view.  To prevent that from happening, leave the query in SQL view and make sure you save it that way.  You can switch to QBE view but just remember to not save the query from that view.
0
 

Author Comment

by:SteveL13
ID: 39806600
I appreciate your response.  Thank you.  But somehow I have to be able to create a query using information the user entered into the form.

I've attached a screen shot of the form.  If the user leaves every field blank then they get all records.  If they enter, for example "83" in the Bay field then they get the records that have 83 in the bay field in the table.

If the enter a category then they get all records in that category.

If they enter a category and a start date and an end date, they get all records with that category and a Date between the start date and end date.

Isn't there a way I can do this?

--Steve
Selection-Form.jpg
0
 
LVL 36

Accepted Solution

by:
PatHartman earned 500 total points
ID: 39806718
The example I showed you does that.  It is EXACTLY what you need.  Just substitute your own column names.

The "fld1 = Forms!yourform!fld1" part of the expression selects records that match the value from the form.  The "OR Forms!yourform!fld1" part of the expression checks the form field for null and if it is null, the expression returns true which will select ALL values.

The "AND" operators that connect the expressions combine them so that if you enter something in more than one field, they all must be true.

By combining the (xx OR yy) AND (aa OR bb) AND (cc OR dd) sets in the query, your SQL is static and you don't need to build it on the fly.
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question