Access parameter query with optional second parmeter - ms-access

I have an ACCESS database that has a table with date as one of the fields. I can create a form that allows the user to enter a start date and end date and then use those in a query to filter the date for only records between those dates. But I would like to make the end date optional so if the user would only enter the start date the query would return on records greater than that date. I am trying to do this with one query and without getting into VBA but not sure if this can be done.
I tried something like this but it did not work...I got error message saying the syntax was not correct or I got no results at all.
In the date field criteria I tried
IIF(isNull([Forms]![frmdateselect]![enddate]),
(>=DateValue([Forms]![frmdateselect]![startdate])),
((>=DateValue([Forms]![frmdateselect]![startdate])) AND
(<=DateValue([Forms]![frmdateselect]![enddate]))))
Any help would be great

Have you tried replacing the IsNUll with the Nz function?
IIF(Nz([Forms]![frmdateselect]![enddate]),0),
([Forms]![frmdateselect]! [enddate]),
(>=DateValue([Forms]![frmdateselect]![startdate])),
((>=DateValue([Forms]![frmdateselect]![startdate])) AND
(<=DateValue([Forms]![frmdateselect]![enddate]))))

Related

MS Access Form to Query with Date Ranges (And Even Without)

I'm new to Access and I'm building a database here at work to log all production that was done. I was able to make a query form with criteria between a date range, condition, part number and work order. Using a code in the expression builder, these are what is placed in the criterion:
Date range: Between [Forms]![Form Query]![BeginDate] And [Forms]![Form Query]![EndDate]
Part number: Like (IIf(IsNull([Forms]![Form Query]![Part Number]),"*",[Forms]![Form Query]![Part Number]))
Condition: Like (IIf(IsNull([Forms]![Form Query]![Condition]),"*",[Forms]![Form Query]![Condition]))
This is where even when part numbers and condition is empty, the query will display all records. My problem is the date range if I leave it empty (say, I simply wanted to query all of the records), it will return with zero value. I wanted it to make it simple for the user that if I leave the date range empty, it will still show all of the records. I had to temporarily put the date range as required to always return results.
Query form
Query Criteria
All input is appreciated! Thank you so much!
Provide alternate value if date is not input:
Between Nz([Forms]![Form Query]![BeginDate], #1/1/1900#) And Nz([Forms]![Form Query]![EndDate], #12/31/2200#)
For text fields, assuming value is unique and is never part of a longer string, LIKE criteria can just concatenate wildcard:
LIKE [Forms]![Form Query]![Part Number]) & "*"

Access query using calculation

I'm trying to make a query using a calculation with Date().
I have a field named [Currentordue] and a field named [duedate]. What I'm trying to accomplish is making a query to limit the results by "if [currentordue] equals "due" or if [duedate] minus today's date is less than 30 days."
I've tried a few different ways but always seem to end with either an error or with no results showing (which would be an error as well since I know there are fields that are due).
Any and all help would be appreciated.
Here is a way to use two different date conditions:
SELECT Table1.Currentordue, Table1.duedate, DateDiff("d",[duedate],Date()) AS Expr1
FROM Table1
WHERE (((DateDiff("d",[duedate],Date()))<30)) OR (((Table1.Currentordue)=[duedate]));

MS Access - Use result from query to calculate field value

I'm trying to pull some data from a query in my database into a calculated field in a table. I have dates entered for some jobs I'm recording (DateCallOpened, DateQuoteSent, DateQuoteReceived), as well as WorkType for each job to track the type of work done. I've used calculated fields to find the time it took for each record between those dates. I've also used qryTimings to find the average length of time for the WorkType.
I'd like to build fields that showed the ProjectedQuoteSent, and use the data from my query to calculate the date I can expect the quote to be sent, but I just can't figure out how to pull that data out of the query. I was hoping it would be something as simple as:
=[DateCallOpened]+[qryTimings]:[Avg Of TimeToSendQuote]
You can use a DLookup() function to grab your value from your query. So your formula would be something like:
=[DateCallOpened]+DLookup("Avg Of TimeToSendQuote", "qryTimings", _
"[WorkType]=" & [Forms]![frmMyForm]![txtWorkTypeInput])
See this for more info.

Access VBA filtering SQL by short date on a long date/time field

I am having an issue getting criteria to work. There is a related question on StackO here, but I have tried what's in it and it's still not working, so I'm asking my specific question. Apologies if that's not technically kosher at StackO.
Can't filter MS access datetime field using short date
I have a query that I need to have WHERE criteria based on an associate ID and the current day’s date. But the field that has the current day’s date needs to be a long date and time field, so criteria for short date doesn’t work. I have made this query in query design mode and it does exactly what I want:
SELECT tbl_Data.[#], AssocID, tsUpdated FROM tbl_Data WHERE AssocID = 4441 AND DateValue([tsUpdated])=Date()));
But when I do the equivalent in VBA, which I need to have this happen in, it does not work. Please note, I have used Date in this vba version because according to this website, Date will return current date:
Set FinishReport = CurrentDb.CreateQueryDef("qry_SessionReport", "SELECT tbl_Data.[#], AssocID, tsUpdated FROM tbl_Data WHERE AssocID = 4441 AND DateValue([tsUpdated])=Date”)
I have tried all kinds of syntax, and I have tried the answers from that StackO URL above, but nothing has worked. Any thoughts?
Thanks!
For the querydef, I noticed you don't have the closing parenthesis for the Date() function in the SQL text. In the VBA editor, the function is listed as Date with no parenthesis but if you tried to use the Date() function in a query without the parenthesis Access would throw an error. Hope that helps!

Access Report - Show Week Ending Date

I have an Access 2000 report based on a query like this
SELECT
...
FROM Clients AS wc INNER JOIN ...
WHERE ((wo.OfferStatusID)=3) AND
((DatePart("ww",[wo.StatusTimeStamp]))=DatePart("ww",[Enter Week End Date]))
AND ((Year([wo.StatusTimeStamp]))=Year(Date())));
The where clause allows you to enter the 'Week End Date' and it finds all of the records for the Sunday-Saturday week that contains the end date.
My problem is I need to include the Saturday end date on the report so that the header reads '... for week ending 5/9/09' and I can't figure out how to get that date without the query asking me for it a second time.
Is there a way to force Access to return the parameter entered as another field in the results, or another way to get that week ending date?
Continuing to poke around in the query designer I discovered that I could add this to the SELECT clause and get the value entered added to each row:
[Enter Week End Date] AS WeekEndDate
This works, but I am still open to other suggestions.
You could stack two queries (make one the source of the other). This would be pretty MS Access'y. However, if you have it working now, I'd stick with what you have. It's probably cleaner.
Another approach is to use a form to grab the query params first, and reference the form control in the query. For instance, with your example, create a form called frmGetDateParam, and add a textbox called txtDate. Change the format to a date, perhaps add some validation, doesn't matter. Add a command button to the form which opens up the report. Then, change your query to look like this :-
SELECT
...
FROM Clients AS wc INNER JOIN ...
WHERE ((wo.OfferStatusID)=3) AND
((DatePart("ww",[wo.StatusTimeStamp]))=DatePart("ww",Forms!frmGetDateParam!txtDate))
AND ((Year([Forms!frmGetDateParam!txtDate]))=Year(Date())));
You can also reference Forms!frmGetDateParam!txtDate as a field on your report.
The only downside to this approach is that if you try to open the query/report without the parameter form being open, then the query will still prompt you for the date value.