Search form with date range or all if values are null

pjmatelli

Board Regular
Joined
Oct 13, 2005
Messages
206
Hi all,
I have a search form whereby the users of my database can pick a username, a facility, a vendor, etc. They can also further filter by status and/or date range. So if you pick a user, all POs for that user show up, if you pick a status, it filters the user list to just show those POs with the proper status. If you then pick a date range, I want it to further filter the list to include only those POs within the date range. It works fine as long as dates are entered, but if no dates are entered, I want it to return all values. The help needed is with how to use the between date option within the query so that if the date range is left blank, all POs matching the other criteria are returned. I have attempted this many different ways to no avail. I hope I made sense. Any help as always is greatly appreciated.

TIA
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Paula

What do you have so far?

One way to deal with this could be to check if no date(s) have been entered and if they haven't use some date way in the past and one way in the future.

How to implement that, or any other method, depends on how you have things set up.
 
Upvote 0
Thank you for the responses...I was temporarily re-routed to another project and now am back on this one...Rupert, I had seen that demo on the form before, but that is only the start of my issue. Norie, I have an OrderDate on all POs. On my search form, users can search by the following key items: user, vendor, department or facility. The secondary items are: POType, Status, and DateRange. Everything works fine and my queries are using the Like & * option on all items. Once I get to the date range, the search form only likes it if the start and end dates are the same. So I am not sure how to set the criteria of the order date to work properly...here is what I have: Like [forms].[frmSearchPO].[txtstartdate] & "*" And Like [forms].[frmSearchPO].[txtenddate] & "*"...I guess my real question is how to use the Between And option with the Like???? Hope I made sense.

Thanks again...
 
Upvote 0
Paula

The answer is not to use Like.

Like is a comparison operator used to compare strings.

I've never seen it used with dates or numbers and I can't really see how it would work.

What fields are you actually using in your criteria?
 
Upvote 0
make a new query
view it in SQL View
put this in there, but change the name of your_table to whatever it should be

Code:
select  
    your_table.*
from 
    your_table

where  
(
  iif 
  ( 
      [forms]![frmSearchPO].[txtstartdate] <> "" and [forms]![frmSearchPO].[txtenddate] <> "" ,
      your_table.OrderDate between [forms]![frmSearchPO].[txtstartdate] and [forms]![frmSearchPO].[txtenddate], 
      1 = 1 
  ) 
  and 
  iif 
  ( 
      [forms]![frmSearchPO].[txtvendor] <> ""  ,
      your_table.Vendor = [forms]![frmSearchPO].[txtvendor] , 
      1 = 1 
  ) 
)
that should work,
then you can put it back in Design View to see how Access shows it in the query designer

I find it easier to read in SQL View though

so save that query and use it as the RecordSource for the form; put a button or something on the form that the user requests the data with
if the user has put something in the text boxes for the start and end dates, then the query will use it, but if either text box is blank, then the "where clause" of the query will be
where 1 = 1
which is always true, so all rows would be returned if either text box is blank

and you can also include vendor in your criteria that way too
 
Last edited:
Upvote 0
Thank you so much...I will give that a try today and let you know...I really do appreciate the time and effort.
 
Upvote 0

Forum statistics

Threads
1,221,834
Messages
6,162,268
Members
451,758
Latest member
lmcquade91

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top