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

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]) & "*"

Related

Like Nz not handling a null value

I have a query that I control with some filtering options using a form. One of those filters is a medical code search. Sometime this field has a value and sometimes it is null. I want to be able to search for a partial string of data as well. The code I am trying is:
Expr9: ([tblQA].[Charges Entered]) Like Nz("*" & [Forms]![MainMenu]![btnManagersMenu].[Form]![txtChargeCode] & "*","*")
What I would like to have happen is if I enter some value in txtChargeCode that it will return any record with that string. That part works. The problem comes in when txtChargeCode has no value. If the record is null for Charges Entered then those records are filtered out of the results. I still see records that have a value. I want any null values for Charges Entered in the records to appear in the results, if txtChargeCode is left blank. Can anyone help me with whats going on here? I've read the Like and Nz pages so many times my eyes have crossed.
Your current criteria will never be null because it will always include the string "**".
Instead, we will use Nz to convert null values in [Charges Entered] into empty strings (""). Now that all items in [Charges Entered] are considered to be strings in the criteria, we can compare them with the LIKE operator.
The expression below will return all records (including nulls) when txtChargeCode is blank.
Nz([tblQA].[Charges Entered], "") Like "*" & [Forms]![MainMenu]![btnManagersMenu].[Form]![txtChargeCode] & "*"
You could sort [Charges Entered] so nulls are displayed at the top of the results to make it easier to spot nulls when txtChargeCode is empty.

Access parameter query with optional second parmeter

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]))))

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.

query to filter on specific data or no filter if blank

I have a query which filters records based on dates (start date and end date)selected in a previous form. I want the query to filter the specific date range, or output all records if the fields are left blank.
I am unfamiliar with SQL. is there a way to add an if-then statement?
I can use vba if necessary, but would like to use the Access GUI if it is possible.
If you have a parameter, used in WHERE clause (Criteria in query builder) and you want to show all records if parameter is empty, just add this parameter as new column and OR condition where indicate Is Null or, better add a column with expression Nz([MyParam],"") and in Condition area inORrow add""`. Unfortunately in query builder this construction may be quite complicated if you have few parameters, in SQL it looks much simpler, for instance in your case it will be something like this:
WHERE (MyDate >= [paramDateStart] and MyDate <= [paramDateEnd])
OR (Nz([paramDateStart],"")="" AND Nz([paramDateEnd],"") = "")
Sometimes simpler edit SQL and then switch to Design view
You can use these criteria for StartDate and EndDate respectively to compare them to themselves in case one (or both) of the search fields on the form is empty (Null):
>=Nz([Forms]![YourForm]![FromDate], [StartDate])
<=Nz([Forms]![YourForm]![ToDate], [EndDate])

Odd results in MS Access Query using a DSUM function and parameters

I've just started using MS Access this month and I have a very odd bug. I'm trying to create a query that searches for records in a table that have a maxBenefit (a dsum from a different table's field, with a one to many relationship) within a certain range. I'm using the DSUM function to get the maxBenefit because the table has a dailyBenefits field that need to be added together.
Here is my function:
maxBenefitOfQuote: Nz(DSum("[wholeYearBenefit]","tblDisabilityQuoteDailyBenefits",
"[quoteID] = " & [tblDisabilityQuotes].[ID]))
I know the function works because it produces the correct values. The query also takes in two parameters from a form to create a range for maxBenefits.
I limit the results with this criteria:
>=[Forms]![frmDisabilityFindSimilarQuotes]![minBenefitTotal] And
<=[Forms]![frmDisabilityFindSimilarQuotes]![maxBenefitTotal]
The problem is I get very odd results from the query with maxBenefits outside the range or not returning records with maxBenefits inside the range. If I set the minBenefitTotal to 0 and the max BenefitTotal to 100000000 I get no records returned from the query. If I set the minBenefitTotal to 0 and the maxBenefitTotal to 999999999 I get all the proper records.
Any ideas why this is happening? Thanks in advanced.
First,
Try wrapping both of your inputs in a call to CCur:
>=CCur([Forms]![frmDisabilityFindSimilarQuotes]![minBenefitTotal]) And
<=CCur([Forms]![frmDisabilityFindSimilarQuotes]![maxBenefitTotal])
Next,
If you omit the actual form inputs, and hard-code numbers into the criteria, does it work?
For Example: (>= 0 and <= 100000000)
If that worked... It feels like a string -> number conversion issue. Make sure all string input is actually a number (via CCur()) before sending it into the query.