Solved

Multi field criteria entry form

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

911 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

19 Experts available now in Live!

Get 1:1 Help Now