473,473 Members | 1,581 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

2 parameters query: trouble when specifying query criteria

66 New Member
Hello everyone!

First and foremost, my apologies for the title of the post. It is not very clarifying of what the problem is about, but I didn't know how to put it...
My problem is as follows: I have a parameter query consisting of 2 fields (month and year). The parameter prompts the user to enter the month and the year, that is, [Please enter the month/year]. When both are entered, there is no problem. Nevertheless, users may want only to enter the year, so as to display all records for that year. Then, I have specified the query criteria as follows:

Fields: Month, Year
Display: both displayed
Criteria 1: [Please enter month] and [Please enter year] :::> Month and yr provided
Criteria 2: [Please enter year] :::> Only yr provided.

But in the first case (entering month and year), those records matching with the year but not with the month will also be displayed.
Is there a way to block the query in a way that if month and year are provided, only those records with the matching month and year will be displayed and not those only matching the year? Is code needed?
I feel I am doing something wrong here, with Access itself (without using any code...).

Best regards.
Apr 30 '08 #1
8 2459
Delerna
1,134 Recognized Expert Top Contributor
can you post your query, it might help clarify the result you are after
I thought I understood during the first half but then I got confused in the second half of the question
Apr 30 '08 #2
limperger
66 New Member
The SQL statements:
Expand|Select|Wrap|Line Numbers
  1. SELECT REGISTRO.SURNAME, REGISTRO.NAME, REGISTRO.MONTH, REGISTRO.YEAR
  2. FROM REGISTRO
  3. WHERE (((REGISTRO.MONTH)=[Please provide month]) AND ((REGISTRO.YEAR)=[Please provide year])) OR (((REGISTRO.YEAR)=[Please provide year]))
  4. ORDER BY REGISTRO.SURNAME;
The problem is that if an user provides the month and year the records matching with the month and year provided will display but so will do the records in which only the year matches with the data provided.
My question is whether it is possible to block the query, in a way that when an user provides the month and year, the query will only display those records in which the month AND the year match, and not ALL the records in which the year match. Obviously, I could make 2 different queries but I would like to know if what I outline here is possible (without using code??).

I hope this time I will be understandable. In any case, it is my fault. I tend to explain myself badly... Sorry!

Best regards!
Apr 30 '08 #3
Delerna
1,134 Recognized Expert Top Contributor
Got it now. It wasn't so badly explained at all. The Query helped as I could compare it with your question to understand your meaning

try this, you just need to check that the entered month is null in the second scenario
see below
Expand|Select|Wrap|Line Numbers
  1. SELECT REGISTRO.SURNAME, 
  2.      REGISTRO.NAME, 
  3.      REGISTRO.MONTH, 
  4.      REGISTRO.YEAR
  5. FROM REGISTRO
  6. WHERE REGISTRO.MONTH=
  7. ([Please provide month] AND REGISTRO.YEAR=[Please provide year])
  8. OR 
  9.  
  10.  
  11.  
  12. (REGISTRO.YEAR=[Please provide year] AND [Please provide month] is null)
  13.  
  14.  
  15.  
  16.  
  17. ORDER BY REGISTRO.SURNAME;
  18.  
  19.  
Apr 30 '08 #4
NeoPa
32,556 Recognized Expert Moderator MVP
@Delerna - Nice work. I particularly like how you're laying out your SQL in a visible way. Very easy to follow :)

@Limperger - I won't criticise too much as it's clear you're making every effort to post sensibly, helpfully and within the rules. I will just make the point though, to remind you to use [ CODE ] tags whenever you post code. It's much easier to read when posted that way.
May 1 '08 #5
NeoPa
32,556 Recognized Expert Moderator MVP
My version to try :
Expand|Select|Wrap|Line Numbers
  1. SELECT [Surname],
  2.        [Name],
  3.        [Month],
  4.        [Year]
  5. FROM [Registro]
  6. WHERE ((([Please provide month] Is Null) OR ([Month]=[Please provide month]))
  7.   AND  (([Please provide Year] Is Null) OR ([Year]=[Please provide year])))
  8. ORDER BY [Surname]
May 1 '08 #6
Delerna
1,134 Recognized Expert Top Contributor
@Delerna - Nice work. I particularly like how you're laying out your SQL in a visible way. Very easy to follow :)
Thanks NeoPa. I do this out of habit for myself. Bugs and logic faults are difficult enough to find as it is without hiding them in messy code.
Its nice to know that at least one other person finds it easy to read.
That means when I retire my replacement will hopefully be able to follow my code and thats a bonus.
May 2 '08 #7
limperger
66 New Member
Hello everyone!

I have just tried Delerna SQL's code and it works fine. Thank you very much!!
NeoPa: It is the first time I post SQL code and didn't know the rules. My fault entirely! Please accept my apologies for any inconveniences caused.

Best regards
May 2 '08 #8
NeoPa
32,556 Recognized Expert Moderator MVP
No worries.

As I said in my post it was clear you were trying to do it as helpfully as possible :)
May 2 '08 #9

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

Similar topics

6
by: Brad | last post by:
I have an sql query that has specific criteria (like state='PA' or state = 'NJ'...) and would like to be able to have the user specify the criteria dynamically either through the web or from...
0
by: apb18 | last post by:
A bit of query plan strangeness. Suppose you have an inheritance tree such that the columns 'ID' and 'field' appear in the top level table, call that table XXX. tables YYY and ZZZ both inherit...
2
by: Ellen Manning | last post by:
Using A2K. In my table I have the field "Grant" which can have a value or be null. I have a query that counts the number of records and has a Where clause on the Grant field. The query won't...
6
by: Martin Lacoste | last post by:
Ok, before I headbutt the computer... don't know why when I add criteria in a query, I get an 'invalid procedure call'. I also don't know why after searching the help in access, the various access...
1
by: longtim | last post by:
I have been having endless difficulty creating reports/queries that set any relevent parameters from controls in forms. I am creating an application under access 2003 but will target access...
0
by: Zlatko Matić | last post by:
Hi everybody! Recently I was struggling with client/server issues in MS Access/PostgreSQL combination. Although Access is intuitive and easy to use desktop database solution, many problems...
1
by: AJ | last post by:
Folllowing on from a previous post, i have created a stored query as follows. SELECT c.ID, c.Company_Name, p., 1 As QueryNbr FROM (Company AS c LEFT JOIN Sale AS s ON c.ID = s.Company_ID) LEFT...
6
by: jim | last post by:
Is anyone able to provide me with a link to useful documentation or just outright explain to me how to set query parameters dynamically? I'm really new to Access and databases in general but I...
7
by: Ceebaby via AccessMonster.com | last post by:
Hi All Here's hoping someone can help me with this. I have a report based on a query where the criteria for 4 of the fields is set from an unbound form. I want the user to be able to select any...
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.