MS Access Between dates query is not working - sql

I'm an Access DB beginner. I have a database with a SearchForm where the user can enter search criteria, click the Search button and populate the subform with the filtered results.
The query has simple query based on what the user enters in fields in the search form
Like "*" & [Forms]![SampleSearchF]![txtMicrochip] & "*" which work well, but my date filter does not produce any results:
Between [Forms]![SampleSearchF]![DateReceivFrom] And [Forms]![SampleSearchF]![DateReceivTo]
The table fields that the date search is based on are Data Type:Date/Time , Format: Short Date
The Search Form fields are Format Short Date
The subform fields are also Short Date
SearchButton is a requery macro
And when I have the this query criteria in the query, none of the search functions work.
Any suggestions where I could look to solve the issue? Any help is appreciated.
Here is my SQL code for the search query,
FROM IndividualsT INNER JOIN SamplesT ON IndividualsT.AnimalID = SamplesT.AnimalID
WHERE (((IndividualsT.SpeciesName) Like "*" & [Forms]![SampleSearchF]![txtSpeciesName] & "*") AND
((IndividualsT.Microchip) Like "*" & [Forms]![SampleSearchF]![txtMicrochip] & "*") AND
((IndividualsT.Name) Like "*" & [Forms]![SampleSearchF]![txtName] & "*") AND
((SamplesT.Location) Like "*" & [Forms]![SampleSearchF]![txtLocation] & "*") AND
((SamplesT.SampleReceived) Between [Forms]![SampleSearchF]![DateReceivFrom] And [Forms]![SampleSearchF]![DateReceivTo]));
SamplesT
SampleID
AnimalID
SampleReceived
Location
CollectionDate
1
1
18/08/2021
Tassie
10/08/2021
7
1
15/09/2021
Berlin
25/09/2021
13
12
25/09/2021
Sydney
4/09/2021
14
12
24/09/2021
New York
1/09/2021
IndividualsT
AnimalID
SpeciesName
Microchip
Name
1
Parrot
1234
Hugo
12
Koala
853
Sherlock

Likely, your issue is the WHERE logic when form fields are empty. When empty, LIKE expressions return as ** which means anything, so no rows are filtered out. However, empty dates conflict with BETWEEN clause. Consider using NZ to return the column itself if form fields are empty:
(
SamplesT.SampleReceived
BETWEEN NZ([Forms]![SampleSearchF]![DateReceivFrom], SamplesT.SampleReceived)
AND NZ([Forms]![SampleSearchF]![DateReceivTo], SamplesT.SampleReceived)
);

Always specify the data type of the parameters:
Parameters
[Forms]![SampleSearchF]![txtSpeciesName] Text ( 255 ),
[Forms]![SampleSearchF]![txtMicrochip] Text ( 255 ),
[Forms]![SampleSearchF]![txtName] Text ( 255 ),
[Forms]![SampleSearchF]![txtLocation] Text ( 255 ),
[Forms]![SampleSearchF]![DateReceivFrom] DateTime,
[Forms]![SampleSearchF]![DateReceivTo] DateTime;
Select *
From YourTable
Where ...

Related

Use a parameter in SSRS for table_name

I have scoured the internet for options and the only one I have found that can do it is by using a $Proc however I am trying to avoid that.
I would think it would be pretty simple to use a parameter to select a different table depending on what the user chooses from a drop down.
Here it is:
- There are two tables the report needs to use,
* some_table_CY (current year table)
* some_table_STLY (same time last year table)
So I created a parameter that gives the user the option to select "Current_Year" or "Last_Year", depending on which one the user chooses the parameter would then be used in the select statement, something like this: "SELECT * FROM :pReportVersion"
However, it is not working. I need it to do this, not using a union since unioning these two tables causes HUGE performance issues and the query takes more than 4 hours to run which is not acceptable for a report that users need on request.
(This is querying oracle)
Use the Dataset expression and set it to:
="SELECT * FROM " & Parameters!ReportVersion.Value
For longer queries you may need to wrap each line with quotes, append with an ampersand and add a line feed:
="SELECT * " & VBCLRLF &
"FROM " & Parameters!ReportVersion.Value & VBCRLF &
"WHERE FIELD1 > 10 " & VBCRLF &
"AND FIELD2 = 'YES' "
you can still use the union..
Say you have a parameter called #year
set the available values to the following (specify values)
current year for label and 1 for value
last year for label and 2 for value
Then your dataset can be something like this:
select * from some_table_CY
where #year = 1
union all
select * from some_table_LY
where #year = 2

Using DCount in Access with multiple criteria

I am trying to count the number of records in a table which satisfy criteria for two different fields. Both fields are string values.
The first field is what type of Test appears e.g. 'Manometry'. I can get this field to work on it's own.
I experience a problem when trying to add the second criteria.
The second field is the TestID, which is in the format A_155_19, where 155 is the investigation number and 19 identifies the year it took place.
I would like to count all the manometry tests which occur in the current year.
DCount("[Test]", "Visits", "[Test] = 'Manometry'" & "[TestID] = *Right((Year(Date)), 2)'")
I am currently getting the error message 3075, which is missing syntax.
Any help would be greatly appreciated.
What about:
DCount("[Test]", "Visits", "[Test] = 'Manometry' And [TestID] Like " & SomeExpression)
You can use Format:
DCount("*", "Visits", "[Test] = 'Manometry' And [TestID] Like '*_' & Format(Date(), "yy") & "'")

MS Access: "Like "*" &_____" criteria preventing criterita from a subsequent field from working, when left blank. Please advise

I created a query in MS Access that references text put into a form to create an expression for criteria to look up records in a table, by either road name or district.
Others will eventually use the form so I used the like wild card to allow for the flexibility of entering an incomplete road name in the form.
Unfortunately this has meant that if one leaves the road name text box empty, the like function displays all records in the database, and does not limit them based on the second criteria (name of a district.)
Should I be using a different function or perhaps writing more complex criteria?
(I have tried removing the wild card, putting the Or functions in each field on different lines or the same line and also thought about adding to an existing macro to limit the query results by district name, if the street name text box was left blank.)
I have been Googling for quite some time and can't figure this one out. Thank you for any help!
Essentially:
The form (called MJidea) has two text entry boxes -
Street (PriStReport)
District (District)
The query set up:
You could change the SQL where clause of your query to:
where
(
[Forms]![MJidea]![PriStReport] is not null and
[Primary Street] like "*" & [Forms]![MJidea]![PriStReport] & "*"
) or
(
[Forms]![MJidea]![PriStReport] is not null and
[Secondary Street] like "*" & [Forms]![MJidea]![PriStReport] & "*"
) or
(
[Forms]![MJidea]![District] is not null and
[Magisterial District] like "*" & [Forms]![MJidea]![PriStReport] & "*"
)
Alternatively, consolidating the first two tests:
where
(
[Forms]![MJidea]![PriStReport] is not null and
(
[Primary Street] like "*" & [Forms]![MJidea]![PriStReport] & "*" or
[Secondary Street] like "*" & [Forms]![MJidea]![PriStReport] & "*"
)
)
or
(
[Forms]![MJidea]![District] is not null and
[Magisterial District] like "*" & [Forms]![MJidea]![PriStReport] & "*"
)

Access: Passing Parameters from Form to Query

I'm writing a query in Access 2013. The query will use the parameters from a form, pass those parameters to the query, and then display the results. I almost have it working. The problem with this form is that there are multiple date fields. There are two text fields (txtFromDate and txtToDate).
The idea is that user can enter date range one of two ways:
the user can click the date picker and select a date range to pass to the query;
the user can choose the Fiscal Year from a combobox.
I have created a separate table that is setup as follows:
Table: tbl_FiscalYear
Columns: FiscalYr_ID, FiscalYearName, FromDate, ToDate
In my query, the results work great (all parameters passed to query) if the user enters the manual dates in the txt fields. However, if they bypass the manual date entry and use the Fiscal Year combobox, the query results ignore all previous parameters and the results only reflect the appropriate date range per the Fiscal Year date setup in the fiscal year table.
I think the culprit is the OR statement at the end. Is there a way to incorporate both date search parameters into the form, and allowing the use of one or the other? Is my code not correct since it doesn't allow passing null values from the text date fields?
SELECT tbl_QUOTE.QUOTE_ID,
tbl_QUOTE.QUOTE_DATESTART,
tbl_QUOTE.QUOTE_DATEEND,
tbl_QUOTE.QUOTE_lookup_POC_ID,
tbl_QUOTE.QUOTE_lookup_LOC_ID,
tbl_QUOTE.QUOTE_lookup_TRAINER_NAME,
tbl_QUOTE.QUOTE_lookup_STATUS_ID,
tbl_QUOTE.QUOTE_lookup_TRAINER_ID,
tbl_QUOTE.QUOTE_lookup_MODS_ID,
tbl_POC.POC_AGENCY_lookup,
tbl_QUOTE.QUOTE_FINANCIAL_CD,
tbl_FISCALYEAR.FISCALYEARNAME
FROM tbl_fiscalyear, tbl_QUOTE
INNER JOIN tbl_POC ON tbl_QUOTE.QUOTE_lookup_POC_ID = tbl_POC.POC_ID
WHERE Nz([QUOTE_lookup_STATUS_ID],"") Like [Forms]![frm_QuoteReport]![cboStatusLookup] & "*"
AND Nz([tbl_POC].[POC_AGENCY_lookup],"") Like [Forms]![frm_QuoteReport]![cboAgency] & "*"
AND Nz([QUOTE_lookup_POC_ID],"") Like [Forms]![frm_QuoteReport]![cboPOC] & "*"
AND Nz([QUOTE_lookup_MODS_ID],"") Like [Forms]![frm_QuoteReport]![cboCourse] & "*"
AND Nz([QUOTE_lookup_LOC_ID],"") Like [Forms]![frm_QuoteReport]![cboLocation] & "*"
AND Nz([QUOTE_lookup_Trainer_ID],"") Like [Forms]![frm_QuoteReport]![cboTrainer] & "*"
AND Nz([Fiscalyr_ID],"") Like [Forms]![frm_QuoteReport]![cboFY] & "*"
AND Nz([Quote_Financial_Cd],"") Like [Forms]![frm_QuoteReport]![chkFundCd] & "*"
AND [QUOTE_DATESTART] Between [Forms]![frm_QuoteReport]![txtFromDate]
And [Forms]![frm_QuoteReport]![txtToDate]
OR [QUOTE_DATESTART]
BETWEEN (SELECT [tbl_fiscalyear].[fromdate]
FROM tbl_fiscalyear
WHERE fiscalyr_ID = [Forms]![frm_QuoteReport]![cboFY])
AND (SELECT [tbl_fiscalyear].[todate]
FROM tbl_fiscalyear
WHERE fiscalyr_ID = [Forms]![frm_QuoteReport]![cboFY]);
I believe I resolved it. I edited the final lines as follows:
OR [QUOTE_DATESTART] Between [Forms]![frm_QuotesReports]![txtFromDate]
And [Forms]![frm_QuotesReports]![txtToDate] <br/> AND [QUOTE_DATESTART]
BETWEEN (SELECT [tbl_fiscalyear].[fromdate] FROM tbl_fiscalyear WHERE
fiscalyr_ID = [Forms]![frm_QuotesReports]![cboFY]) AND (SELECT
[tbl_fiscalyear].[todate] FROM tbl_fiscalyear WHERE fiscalyr_ID = [Forms]!
[frm_QuotesReports]![cboFY])

How to easily filter my query

I am trying to filter a query via a textbox on a form. I have this in the criteria of the query:
Like "*" & [Forms]![Form_Name]![Textbox] & "*"
However if the textbox is blank it only shows data in the query that have got text in the field. It doesn't show all data, i.e. the fields that have no data. Is this possible to do?
The only way I have found to do it is to have 2 queries 1 with the filter and 1 without and run code that if the textbox is empty swap the query.
Just append a zero-length string to the field for the compare, then none of the fields will be null, so they will match *.
WHERE MyField & ""
Like "*" & [Forms]![Form_Name]![Textbox] & "*"
I don't have a copy of Access available to me at the moment but a quick google and some tinkering tells me that this is likely to do what you need:
Like "*" & [Forms]![Form_Name]![Textbox] & "*"
Or (Len(Nz([Forms]![Form_Name]![Textbox],"")) = 0 And Len(Nz([Column],"")) = 0)
Sources:
http://bytes.com/topic/access/answers/607087-validation-if-null-empty-string
http://office.microsoft.com/en-001/access-help/table-of-operators-HA010235862.aspx
http://www.techonthenet.com/access/functions/advanced/nz.php