?
Solved

Multi field criteria entry form

Posted on 2014-01-23
3
Medium Priority
?
632 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 39

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 39

Accepted Solution

by:
PatHartman earned 2000 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

649 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