473,414 Members | 1,928 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,414 software developers and data experts.

Query "or" help needed.

72
In Access 2003 I have a query where it asks the user for two parameter using [Enter Emp #] and [Enter Task Type] pop up. However, I need for the query to return all tasks for the Emp# if TaskType is left blank. I tried "In" in the or section of the query, but that returned all records for all emp#s....unless I used incorrectly.

Thxs for help.
Mar 26 '07 #1
23 1661
MMcCarthy
14,534 Expert Mod 8TB
Try something like this.

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM TableName
  2. WHERE ([Emp#] =  [Enter Emp #]
  3. AND [Task Type] = nz([Enter Task Type],"*")
  4.  
Mary
Mar 26 '07 #2
Rotor
72
Try something like this.

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM TableName
  2. WHERE ([Emp#] =  [Enter Emp #]
  3. AND [Task Type] = nz([Enter Task Type],"*")
  4.  
Mary
Thank you Mary, will give it a shot.
Mar 27 '07 #3
NeoPa
32,556 Expert Mod 16PB
Neat idea but I'd use the LIKE in place of the '='.
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM TableName
  2. WHERE ([Emp#]=[Enter Emp #])
  3.   AND ([Task Type] Like Nz([Enter Task Type],'*')
Mar 28 '07 #4
Rotor
72
Neat idea but I'd use the LIKE in place of the '='.
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM TableName
  2. WHERE ([Emp#]=[Enter Emp #])
  3.   AND ([Task Type] Like Nz([Enter Task Type],'*')
Okay, that worked.

And you guys are going to kill me. Lets say I wanted to filter out certain task types when the field is left blank. The tasks I am interested in tracking are PT, ET, UT. So, with the this code the user can either input ET in [Enter Task Type] or leave bank and the code returns all task types for the user. That is good, but what would be perfect if it only returned ET, PT, UT for the user if [Enter Task Type] is left blank. I know I did not have this in my original question, but at the time it did not dawn on me.

Thank you for any extra help, you guys have been great.
Mar 28 '07 #5
NeoPa
32,556 Expert Mod 16PB
This gets a little more oblique, but can be done. Try :
Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. FROM [TableName]
  3. WHERE ([Emp#]=[Enter Emp #])
  4.   AND IIf(IsNull([Enter Task Type]),
  5.           [Task Type] In('ET','PT','UT'),
  6.           [Task Type]=[Enter Task Type])
This one you owe me a pint for :D
BTW I tested the concept and it worked perfectly.
Mar 28 '07 #6
MMcCarthy
14,534 Expert Mod 8TB
This gets a little more oblique, but can be done. Try :
Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. FROM [TableName]
  3. WHERE ([Emp#]=[Enter Emp #])
  4.   AND IIf(IsNull([Enter Task Type]),
  5.           [Task Type] In('ET','PT','UT'),
  6.           [Task Type]=[Enter Task Type])
This one you owe me a pint for :D
BTW I tested the concept and it worked perfectly.
Nice solution Ade!
Mar 28 '07 #7
Rotor
72
Well Neo, where can I send that pint?

Worked like a champ.

One more question :).

Is this coding something that can be learned by book or some sort of intermediate course on Access? Or is it code that one would have to take VBA courses to learn?

Just looking ahead.
Mar 28 '07 #8
MMcCarthy
14,534 Expert Mod 8TB
Well Neo, where can I send that pint?

Worked like a champ.

One more question :).

Is this coding something that can be learned by book or some sort of intermediate course on Access? Or is it code that one would have to take VBA courses to learn?

Just looking ahead.
This is actually a combination of SQL code and Access functions. It is pretty standard as far as the SQL is concerned and the IIf function is a commonly used Access function.

The Access help file is a good place to learn about Access functions. SQL is a little more complicated.
Mar 28 '07 #9
NeoPa
32,556 Expert Mod 16PB
Well Neo, where can I send that pint?

Worked like a champ.

One more question :).

Is this coding something that can be learned by book or some sort of intermediate course on Access? Or is it code that one would have to take VBA courses to learn?

Just looking ahead.
Mary just beat me into this one. I thought I'd get home before answering properly ;)
Actually the Access Help file also has a full set of help for Access SQL if you can find it.
Really, the SQL I posted was just basic stuff. It was the concept that is a bit out of the ordinary. I simply used the IIf() function to return boolean values rather than numeric or string values which are more commonly used. That is just an experience thing really, and a willingness to look for the less obvious solutions sometimes.
In this case (where IIf() is used within a SQL string), the IIf() function is actually SQL code rather than VBA. It has an important difference from the VBA version, which is that it will only evaluate the required leg of the function.
Mar 28 '07 #10
Rotor
72
Okay,

I tried the same code on another application where if user enters [Enter Emp #] and if [Enter Date dd/mm/yy] is left blank, displays records with current date.

This is what I did:
Expand|Select|Wrap|Line Numbers
  1. WHERE ([emp#]=[Enter Emp #]) 
  2. AND Like Nz([Enter Date dd/mm/yy],'Date()')
So, that returns current date in report but no records are returned for employee or anyone else.

Obviously I did not do it right, so where did I miss the boat?
Mar 28 '07 #11
MMcCarthy
14,534 Expert Mod 8TB
Okay,

I tried the same code on another application where if user enters [Enter Emp #] and if [Enter Date dd/mm/yy] is left blank, displays records with current date.

This is what I did:
Expand|Select|Wrap|Line Numbers
  1. WHERE ([emp#]=[Enter Emp #]) 
  2. AND Like Nz([Enter Date dd/mm/yy],'Date()')
So, that returns current date in report but no records are returned for employee or anyone else.

Obviously I did not do it right, so where did I miss the boat?
Remove the single quotes from around Date(). It's a function, not a string.

Otherwise, very good attempt.
Mar 28 '07 #12
Rotor
72
Remove the single quotes from around Date(). It's a function, not a string.

Otherwise, very good attempt.
Tried that, getting syntax error. Here is actuial snippet of code from teh query:
Expand|Select|Wrap|Line Numbers
  1. Where (((QryjoinCompjobswithempquery.[Employee #])=[Enter Emp #] AND ((tblJobsComp.[Created Date Local])=Nz[Enter Date mm/dd/yy], Date()));
Where is the syntax error?
Mar 28 '07 #13
MMcCarthy
14,534 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. Where (((QryjoinCompjobswithempquery.[Employee #])=[Enter Emp #])
  2. AND ((tblJobsComp.[Created Date Local])=Nz([Enter Date mm/dd/yy], Date())));
  3.  
You didn't have an opening bracket after nz. Try the above.

Mary
Mar 28 '07 #14
Rotor
72
Expand|Select|Wrap|Line Numbers
  1. Where (((QryjoinCompjobswithempquery.[Employee #])=[Enter Emp #])
  2. AND ((tblJobsComp.[Created Date Local])=Nz([Enter Date mm/dd/yy], Date())));
  3.  
You didn't have an opening bracket after nz. Try the above.

Mary
Ding!

Thank you very much.

Great stuff, if it is any consolation I am off for the rest of the week, so I wont pester you guys any more...until then :).
Mar 28 '07 #15
MMcCarthy
14,534 Expert Mod 8TB
Ding!

Thank you very much.

Great stuff, if it is any consolation I am off for the rest of the week, so I wont pester you guys any more...until then :).
No problem. Glad you've got it working.

Mary
Mar 28 '07 #16
NeoPa
32,556 Expert Mod 16PB
Ding!

Thank you very much.

Great stuff, if it is any consolation I am off for the rest of the week, so I wont pester you guys any more...until then :).
Until then, then :) (What did I just let myself in for :D)
Mar 28 '07 #17
Rotor
72
Until then, then :) (What did I just let myself in for :D)
Hiya Neo,

One more question on this subject. I would like to take this code a little bit further and maybe you or mccarthy can tell me how I can make this work if possible:

Expand|Select|Wrap|Line Numbers
  1. WHERE (((tblEmployees.[Employee Number])=[Enter Emp #])
  2.   And IIf(IsNull([Enter Task Type]),[Task Type] In ('Borescope','ET','PT','UT','RT'),[Task Type]=[Enter Task Type])
  3.   And ((tblAcftJobsComp.[Created Date Local])>=[From Dte mm/dd/yy]));
To the above code I would like to add
Expand|Select|Wrap|Line Numbers
  1. =Nz([Enter Date mm/dd/yy], DateAdd("d", 30, Now))))
I am not sure how to work it in with the >= comparison operator. I am asking the user from what date he wants to display the records. However, based on previous ideas, if he leaves the field blank, I would like the query to return the past 30 days.
Apr 2 '07 #18
NeoPa
32,556 Expert Mod 16PB
I can't get to this now as it's been hectic all day.
It looks like you're on the right lines - I'll have a look later at finalising the details for you :)
Apr 2 '07 #19
NeoPa
32,556 Expert Mod 16PB
Try using :
Expand|Select|Wrap|Line Numbers
  1. ...>Nz([Enter Date mm/dd/yy], DateAdd("d", -30, Date()))
Let me know if that works. If not then post your current SQL code but try to lay it out so that I can read it without copying it to a text-editor first.
Apr 3 '07 #20
Rotor
72
Try using :
Expand|Select|Wrap|Line Numbers
  1. ...>Nz([Enter Date mm/dd/yy], DateAdd("d", -30, Date()))
Let me know if that works. If not then post your current SQL code but try to lay it out so that I can read it without copying it to a text-editor first.
Good Morning Neopa,

I tried the above and I am getting a syntax error. Here is the SQL:

Expand|Select|Wrap|Line Numbers
  1. And ((tblAcftJobsComp.[Created Date Local])
  2. >Nz([Enter Date mm/dd/yy], DateAdd("d", -30, Date()));
  3.  
Hope I layed it out okay for ya, wasnt sure what you meant, so I assumed to mean to break it up in the window.
Apr 3 '07 #21
NeoPa
32,556 Expert Mod 16PB
It's hard without the full SQL (Well laid out by the way - perfectly readable), but immediately obvious is that you've introduced an unclosed opening parenthesis '('. Try balancing them all up properly first, then, if that won't work, post the full SQL & I'll check it for you. If there's anything more to the error message that would be helpful too.
Apr 3 '07 #22
Rotor
72
It's hard without the full SQL (Well laid out by the way - perfectly readable), but immediately obvious is that you've introduced an unclosed opening parenthesis '('. Try balancing them all up properly first, then, if that won't work, post the full SQL & I'll check it for you. If there's anything more to the error message that would be helpful too.
Okay, parenthesis fixed. Thxs.

Now to deal with that other thread :). I am going to address the field naming issue foist, then get back to you with the rest.
Apr 3 '07 #23
NeoPa
32,556 Expert Mod 16PB
Sounds fine.
Well done for fixing the parentheses :) Glad to hear it fixed the problem.
Apr 3 '07 #24

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

Similar topics

0
by: Greg Strong | last post by:
Hello All, In the past I've used a combo box with the 'row source' being an Access SQL union query to select "All" or 1 for only 1 criteria in a query. An example is as follows: SELECT 0 As...
2
by: Julio Allegue | last post by:
I am getting the wrong Count(*) on vb.net using the ExecuteScalar . It returns all the rows. It doesn't seem to look at the WHERE clause. At the same time, I am getting the correct count on "SQL...
5
by: comp.lang.php | last post by:
$orderBy = 's.app_date desc, s.last_name asc, s.first_name asc, s.mi asc'; if ($_REQUEST) { $ascArray = array('asc' => 'desc', 'desc' => 'asc'); // ARRAY OF ALL ORDERING POSSIBILITIES $junk =...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
3
by: Johs | last post by:
When I declare a string in C++ I type: std::string mystring = "sdfsdf"; afterwards I can access string methods like: mystring. but why is there both :: and . operators and what are the...
5
by: lsllcm | last post by:
Hi All, I have one question about many "or" operation make system choose incorrect index There is one table TT ( C1 VARCHAR(15) NOT NULL, C2 VARCHAR(15) NOT NULL, C3 VARCHAR(15) NOT NULL,...
13
by: Robertf987 | last post by:
Hi, Yet another thing I need help with I'm affraid. I'll first explain what I want, then I'll try to explain what I have. I'm using Microsoft Access 2000. What I want is to be able to do a...
14
by: kpfunf | last post by:
I have a select query that uses an "OR" join. With that join in place, I cannot edit the data in the query datasheet; if I delete the "OR" and only use one join, then I can edit the data. I read...
0
by: jonceramic | last post by:
Hi All, My apologies for asking something that I'm sure has been answered for. But, my google searching can't find a proper set of keywords. I would like to add "New..." or "other..." or...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.