Solved

Multi field criteria entry form

Posted on 2014-01-23
3
622 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 34

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 34

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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now