473,387 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Access 2010 - Combine query for specific year and query for between two years

Hi everyone and thank you in advance.

I am working on a user form where you as part of the filtering process can enter a single year or an interval between two years.

I have three unbound fields
1- FindYearSpecific
2- FindYearStart
3- FindYearEnd

...and I am using the query builder cause I need to query other fields as well, so I would like to keep doing that instead of writing VBA (but that might be a mistake, I don't know...)

To find the specific year I have the following code:
Expand|Select|Wrap|Line Numbers
  1. Like[Forms]![frmSearchGeneral].[FindYearSpecific] & "*"
This works just fine and give me all the records if I don't specify a year. This is exactly what I want

Then I have the following code for the year range:
Expand|Select|Wrap|Line Numbers
  1. Between IIf([Forms]![frmSearchGeneral].[FindYearStart] Is Null,#01-01-1000#,[Forms]![frmSearchGeneral].[FindYearStart]) And IIf([Forms]![frmSearchGeneral].[FindYearEnd] Is Null,#31-12-9999#,[Forms]![frmSearchGeneral].[FindYearEnd])
A little explanation seems in order cause this is long...
First of all, thank you, NeoPa, for helping with that code. I found it in an unrelated post.
In my top field in the query builder (Field) I have - FindYears: Year ([Date])
I use the extreme dates to make sure that I get all records if the field in the user form is left empty.

This works just fine most of the time. And I am not sure why it is only most of the time.
I have to write in both the fields before the code execute correctly. After that when I manually clears the fields everything works a cording to plan but if I use code to clear the fields like:
Expand|Select|Wrap|Line Numbers
  1. me.FindYearStart = ""
I don't get any records when I run my query.

And on top of that, my real problem is, that I seem unable to combine the two criteria to get anything useful :S

Just to sum up:
I need a query that do the following
1- Checks if I am asking for a specific year
if Yes, then return all records for that year - Then nothing else
if No, continue to 2
2- Checks if I am asking for a year range
If FindYearStart is empty, then returns all records form the beginning of time until year entered in FindYearEnd.
If FindYearEnd is empty, then returns all records form FindYearStart to the end of time.
If there is a year in both fields, then returns the range asked for

I think I am almost there but I really need some help to get the rest of the way.

Thank you in advance and sorry for the wall of text.
Oct 31 '16 #1

✓ answered by jforbes

I think you have two questions listed. We normally limit a thread to one question.
I think the first question is how to perform build a query with multiple criteria and the second is how to reset the Form.

For your where clause, I think your gonna want something like this:
Expand|Select|Wrap|Line Numbers
  1. WHERE Year(SomeYear) = [Forms]![frmSearchGeneral].[FindYearSpecific]
  2.   OR ([Forms]![frmSearchGeneral].[FindYearSpecific] IS NULL 
  3.        AND SomeYear Between IIf([Forms]![frmSearchGeneral].[FindYearStart] Is Null,#01-01-1000#,[Forms]![frmSearchGeneral].[FindYearStart]) 
  4.                     And IIf([Forms]![frmSearchGeneral].[FindYearEnd] Is Null,#31-12-9999#,[Forms]![frmSearchGeneral].[FindYearEnd]
  5.       )
The first part of the OR will return records if the a SpecificDate is selected, where the second part of the OR will only return records if the SpecificDate is Null and there are records found with the Between.


I think for Resetting the Form, you'll want to use:
Expand|Select|Wrap|Line Numbers
  1. me.FindYearStart.Value = Null
The reason for this is that in your query, you are checking for Nulls instead of Zero Length Strings. For more information, check this out: What is the difference between "" and Null?

2 1009
jforbes
1,107 Expert 1GB
I think you have two questions listed. We normally limit a thread to one question.
I think the first question is how to perform build a query with multiple criteria and the second is how to reset the Form.

For your where clause, I think your gonna want something like this:
Expand|Select|Wrap|Line Numbers
  1. WHERE Year(SomeYear) = [Forms]![frmSearchGeneral].[FindYearSpecific]
  2.   OR ([Forms]![frmSearchGeneral].[FindYearSpecific] IS NULL 
  3.        AND SomeYear Between IIf([Forms]![frmSearchGeneral].[FindYearStart] Is Null,#01-01-1000#,[Forms]![frmSearchGeneral].[FindYearStart]) 
  4.                     And IIf([Forms]![frmSearchGeneral].[FindYearEnd] Is Null,#31-12-9999#,[Forms]![frmSearchGeneral].[FindYearEnd]
  5.       )
The first part of the OR will return records if the a SpecificDate is selected, where the second part of the OR will only return records if the SpecificDate is Null and there are records found with the Between.


I think for Resetting the Form, you'll want to use:
Expand|Select|Wrap|Line Numbers
  1. me.FindYearStart.Value = Null
The reason for this is that in your query, you are checking for Nulls instead of Zero Length Strings. For more information, check this out: What is the difference between "" and Null?
Nov 3 '16 #2
Sorry for the double question.
In my head it was a question and a weird quirk...

But you solved it :)
It works perfect!

Thank you so much.

And if it can benefit others:
I wrote the code in my query builder omitting the Year(SomeYear) because in the query builder you put the top field (Field) to:
Expand|Select|Wrap|Line Numbers
  1. Year([FieldWithDate])
So my final code in the criteria field was:
Expand|Select|Wrap|Line Numbers
  1. [Forms]![frmSearchGeneral].[FindYearSpecific]
  2. [Forms]![frmSearchGeneral].[FindYearSpecific] Is Null And Between IIf([Forms]![frmSearchGeneral].[FindYearStart] Is Null,#01-01-1000#,[Forms]![frmSearchGeneral].[FindYearStart]) And IIf([Forms]![frmSearchGeneral].[FindYearEnd] Is Null,#31-12-9999#,[Forms]![frmSearchGeneral].[FindYearEnd])
Spread over two criteria fields.

And it works beautifully :)
Thanks again :)
Nov 3 '16 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: PMBragg | last post by:
Thank you everyone that helped me with my DateSerial question. I really do appreciate it. Now my accountant has told I need to pull the inventory for just the year I'm depreciating. Is there a way...
3
by: DFS | last post by:
I've been working around this for years (I believe), so I figured someone here might know: Why won't a crosstab query accept a value from a form reference? TRANSFORM...
14
by: Darin | last post by:
I have a table that I want to delete specific records from based on data in other tables. I'm more familiar with Access '97, but am now using 2003, but the database is in 2000 format. In '97, I...
3
by: NCRStinks | last post by:
Hi Every1 Having a slight issue on creating a query to combine 2 queries, this year and last years data. The thing is, I have data for this year for all stores, however on last year I dont for...
4
by: pokerboy801 | last post by:
OK, I will try to explain this as clearly and as concise as possible. I am using Access, which has three MS Excel Linked tables, to store call center metrics for reps. My Excel workbook has three...
2
by: TroutmansRegistrar | last post by:
I have a web programming task that has me stumped. I hope that one of you could give me some insight or might know someone who could guide me in the right direction. This is the final piece of the...
5
by: jaad | last post by:
Hello, I have a database that was written in access 2007 on my pc. I wanted to work off site with the database so I uploaded it onto my laptop which is loaded with access 2010 beta. When I...
3
by: john latona | last post by:
hi! i realize that access 2010 does not have the username and password function built into it, so i am trying to build it into my db. i have a "login form" with text fields titled username and...
2
by: Redbeard | last post by:
I need to save my Access 2010 database in 2003 format so that someone can use it that is only running 2003. My database was built using Access 1997 and has been upgraded over the years to 2003, 2007...
1
by: Hom2013 | last post by:
HI, I'm having trouble with my SQL code truncating memo fields in my union query. Here is the code: SELECT Projects. AS Expr1 , Input2012 AS 2012Input , AS Famt , . AS Lamt ,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.