473,387 Members | 1,504 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.

Query Expressions

54
Hi there!

I have a query which when run shows all the special needs records. This query gets its information from a linked ODBC table. I have in the database a seperate table (special needs students) which contains similar information but only for the current academic year (which is then used for various other functions). What I want to be able to do is have in the query some sort of expression that will insert the text "exists in database" into a new field of the query if the record in the link table is already in the table special needs students; and if not, leaving the field blank. I've tried using iif and other things but i'm not very good at it so keep getting syntax errors.

I'm just wondering if this is possible and if so what kind of expression do you use?

If you need any more info let me know.

Help greatly appreciated!
Oct 5 '09 #1

✓ answered by JennDub

g diddy,
I think you're referencing the wrong field in your IIF statement. If you would like find out whether the data in one table (StudentID) exists in the other and display them as the calculated field named "Here", you might want to try to replace your IIF statement in the SQL with:

Expand|Select|Wrap|Line Numbers
  1. IIf(IsNull([SpecialNeedsStudents].STUDENTID), "", "exists in database") AS Here
From your first post, it sounded like you were trying to find out if the Student ID already existed in the database using all the records from the EXTSTUDUNIT table and those that matched in the SpecialNeedsStudent table. If so, you'll need to do a join in your query pulling all records from EXTSTUDUNIT and/or EXTSTUDSPECNEED and those that match from SpecialNeedsStudents. This will allow you to use whether or not the StudentID field is populated to determine if the data already exists in the query.

~JennDub

18 3786
ChipR
1,287 Expert 1GB
Create a query that will show all rows from the link table joined with matching rows from the table [special needs students]. Include a field from [special needs students] in your query, and if there isn't a matching record, that field will be null for that record. Now you can create a new column with:
Expand|Select|Wrap|Line Numbers
  1. IIf(IsNull([special needs students].whateverField, "", "exists in database")
Oct 5 '09 #2
g diddy
54
thanks for the quick response! i get the error the expression contains the wrong number of arguments. The link table has more fields than the special needs students one. sorry i didn't realise. From what I know about databases I think that means it's not possible as they both need the same number of fields to work. Thank you anyway for your help! Much appreciated
Oct 6 '09 #3
NeoPa
32,556 Expert Mod 16PB
The answer Chip supplied is pretty much correct (If I were being picky I may mention the blank value is normally specified as Null instead of ""). If it's not working for you I suspect you may have transcribed it with an error.

Why don't you post what you have and we'll see if we can spot the problem.
Oct 6 '09 #4
g diddy
54
OK thanks NeoPa! I currently have 3 tables that are being used for this query. The link table EXTTSTUDSPECNEED (contains 11 fields), the link table EXTSTUDUNIT (contains 7 fields) and the table SpecialNeedsStudents (contains 13 fields). All 3 are linked together via StudentID. I have 5 fields that the query is picking out: Name, Course and Notes (from SpecialNeedsStudents) StudentID from EXTTSTUDSPECNEED and UNITCODE from EXTSTUDUNIT. Without adding in the IIF statement it works correctly and brings up all the disability students. The IIF statement I used was:
Expand|Select|Wrap|Line Numbers
  1. IIf(IsNull([SpecialNeedsStudents].Here, "", "exists in database")
"Here" is a new field I added to the SpecialNeedsStudents table especially for this query. So all the values in the table are blank (for this field) with the hope that this IIF statement will populate the query field for those students who are in this table and the link table for use in a report.

I hope that helps. Really appreciate the help!
Oct 6 '09 #5
NeoPa
32,556 Expert Mod 16PB
This all sounds very wrong. You cannot update the underlying fields from within a SELECT query.

This posted code is too isolated to tell us much. It is the rest of the SQL that gives the context within which this code can (but maybe doesn't) make sense. Can you post all of the SQL you are using please.
Oct 6 '09 #6
g diddy
54
Certainly, I hope this is what you mean. If not let me know.

Normal query on Access 2003 with the following:
Expand|Select|Wrap|Line Numbers
  1. Field: Name
  2. Table: SpecialNeedsStudents
  3. Sort: Ascending
  4.  
  5. Field: STUDENTID
  6. Table: EXTTSTUDSPECNEED
  7.  
  8. Field: COURSE
  9. Table: SpecialNeedsStudents
  10.  
  11. Field: UNITCODE
  12. Table: EXTTSTUDUNIT
  13. Criteria: [Forms]![SelectModule]![SelectModule]
  14.  
  15. Field: NOTES
  16. Table: SpecialNeedsStudents
  17.  
  18. Field: IIf(IsNull([SpecialNeedsStudents].Here, "", "exists in database") 
  19.  
There are 3 tables (2 link tables, 1 normal) all linked to each other by STUDENTID. The table SpecialNeedsStudents has many fields in the field list but most importantly for this query: STUDENTID, NAME, COURSE, NOTES and Here.

Does this help at all? Thanks for your time!
Oct 6 '09 #7
NeoPa
32,556 Expert Mod 16PB
@g diddy
No, but the following may make it clearer and easier for you to post here in future.

To extract the SQL from a QueryDef (saved Access query) it is only necessary to view the query in SQL View. This shows the underlying SQL for the QueryDef and is text (See Access QueryDefs Mis-save Subquery SQL for problems with SubQueries). When a QueryDef is open (either in Design View or Datasheet View) it is possible to switch to the SQL View simply by selecting View \ SQL View from the menu.

From here it is simple to Copy & Paste it to wherever you need it.
Oct 6 '09 #8
g diddy
54
my apologies! Thanks for that!

Here is the code in sql view

Expand|Select|Wrap|Line Numbers
  1. SELECT SpecialNeedsStudents.NAME, EXTTSTUDSPECNEED.STUDENTID, SpecialNeedsStudents.COURSE, EXTTSTUDUNIT.UNITCODE, SpecialNeedsStudents.NOTES
  2. FROM (SpecialNeedsStudents INNER JOIN EXTTSTUDUNIT ON SpecialNeedsStudents.STUDENTID = EXTTSTUDUNIT.STUDENTID) INNER JOIN EXTTSTUDSPECNEED ON (SpecialNeedsStudents.STUDENTID = EXTTSTUDSPECNEED.STUDENTID) AND (EXTTSTUDUNIT.STUDENTID = EXTTSTUDSPECNEED.STUDENTID)
  3. WHERE (((EXTTSTUDUNIT.UNITCODE)=[Forms]![SelectModule]![SelectModule]))
  4. ORDER BY SpecialNeedsStudents.NAME;
  5.  
Oct 6 '09 #9
NeoPa
32,556 Expert Mod 16PB
I think you need to post the one with your IIf() function call in it. That will give us a clearer idea of what you're attempting to do. The existing SQL has no reference to [SpecialNeedsStudents].Here.

You should understand that your ealier comment related to updating this field is not even in the right ballpark. This is clearly a SELECT query. Updates cannot be done in such a query. You can however, display a calculated value in a SELECT query. We can still look at that.

I feel we're going beyond what we know here without the relevant SQL posted. Let's see that first before we try to proceed.
Oct 6 '09 #10
g diddy
54
My apologies again! It won't let me view as sql view because I keep getting the error:
Expand|Select|Wrap|Line Numbers
  1. The expression you entered has a function containing the wrong number of arguments.
The expression being:
Expand|Select|Wrap|Line Numbers
  1. IIf(IsNull([SpecialNeedsStudents].Here, "", "exists in database")
The sql code I posted before is from the only other things in the query. I have a feeling i'm trying to do something that's not possible and/or i'm going about it entirely wrongly. Is there an alternative method to what i'm doing? My expressions skills are, at most, useless so I may be overly complicating something that could in fact be simple.

I'll try and clarify what i'm trying to do because I realise myself that I am being very confusing! I have a query which picks out information from 3 tables. 2 of the tables are link (ODBC) tables and one is not (SpecialNeedsStudents). I want the query to bring up the results and then add a column into the query (an expression) showing "exists in database" if any of the records found exist in the 2 link tables but not in the SpecialNeedsStudents table. Based upon the sql code I posted before (which is all the other columns of the query), am I going about this all wrongly? (if so I apologise for wasting your time and misleading you!!)
Oct 7 '09 #11
g diddy,
I think that if you change your IIF statement to read:
Expand|Select|Wrap|Line Numbers
  1. IIf(IsNull([SpecialNeedsStudents].Here), "", "exists in database")
the "wrong number of arguments" error will be resolved. When you started your "IsNull" criteria you began with a "(" but did not include an ending ")" after the field name. Once th IIF statement syntax is correct, you should be able to display the calculated field in a Select query or use it to update the table via an Append or Make Table query.

~JennDub
Oct 7 '09 #12
g diddy
54
JennDub you legend thank you very much for spotting that.

Here is the sql view code:
Expand|Select|Wrap|Line Numbers
  1. SELECT SpecialNeedsStudents.NAME, EXTTSTUDSPECNEED.STUDENTID, SpecialNeedsStudents.COURSE, EXTTSTUDUNIT.UNITCODE, SpecialNeedsStudents.NOTES, IIf(IsNull([SpecialNeedsStudents].[Here]),"","exists in database") AS Expr1
  2. FROM (SpecialNeedsStudents INNER JOIN EXTTSTUDUNIT ON SpecialNeedsStudents.STUDENTID = EXTTSTUDUNIT.STUDENTID) INNER JOIN EXTTSTUDSPECNEED ON (EXTTSTUDUNIT.STUDENTID = EXTTSTUDSPECNEED.STUDENTID) AND (SpecialNeedsStudents.STUDENTID = EXTTSTUDSPECNEED.STUDENTID)
  3. ORDER BY SpecialNeedsStudents.NAME;
I'm not sure if i'm going about this right tho. I've added into the table SpecialNeedsStudents for that field (Here) "Exists in Database" as all the records were coming up with a blank field otherwise (- was the iif expression supposed to insert that text if they did exist?). At the moment all the students exist in the database; but if a student record exists in the link ODBC tables but not in the SpecialNeedsStudents table will this work? I don't fully understand all the joins etc so any clafication would be very gratefull.

Thank you for all your time and effort, really appreciate it!
Oct 7 '09 #13
ChipR
1,287 Expert 1GB
Sorry about that missing parenthesis.
You shouldn't be creating a field in the table for this purpose. It should just be a calculated field in a query based on what you already have in the table, as I explained in post #2. You can see the join type by clicking on the line between the ID field of the 2 tables in query design view, and you can change it from Inner Join to Outer Join.
Oct 7 '09 #14
g diddy,
I think you're referencing the wrong field in your IIF statement. If you would like find out whether the data in one table (StudentID) exists in the other and display them as the calculated field named "Here", you might want to try to replace your IIF statement in the SQL with:

Expand|Select|Wrap|Line Numbers
  1. IIf(IsNull([SpecialNeedsStudents].STUDENTID), "", "exists in database") AS Here
From your first post, it sounded like you were trying to find out if the Student ID already existed in the database using all the records from the EXTSTUDUNIT table and those that matched in the SpecialNeedsStudent table. If so, you'll need to do a join in your query pulling all records from EXTSTUDUNIT and/or EXTSTUDSPECNEED and those that match from SpecialNeedsStudents. This will allow you to use whether or not the StudentID field is populated to determine if the data already exists in the query.

~JennDub
Oct 7 '09 #15
NeoPa
32,556 Expert Mod 16PB
Well, I have to say Welcome to Bytes! JennDubb. You seem to be just starting as I watch (I refreshed and you posted again - just your second post to date).

Now I can go on to reading what you said ;)
Oct 7 '09 #16
NeoPa
32,556 Expert Mod 16PB
Jenn is absolutely right here, but Chip has also made an important point. The table you're checking is linked with an INNER JOIN. This means that only records where one exists will be shown anyway. Does that make sense? Is it starting to become clearer or are you still struggling?
Oct 7 '09 #17
g diddy
54
That makes perfect sense. Thank you ChipR, JennDub and NeoPa for all your help. I learnt a lot too. thank you for your time and effort :D
Oct 8 '09 #18
NeoPa
32,556 Expert Mod 16PB
Good for you :)

It's always good when members manage to learn something and make progress. Very gratifying.
Oct 8 '09 #19

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

Similar topics

1
by: news | last post by:
Hi there, I have a table in an Oracle 9i R2 database containing the following string" "It's so easy" When I try to execute the following query: SELECT EXPRESSION FROM EXPRESSIONS WHERE...
2
by: Craig Stadler | last post by:
Im trying to write a like query select * from table1 where column1 like 'abc%' (I realize this syntax isnt correct) I want to include any chars from 0-9 in that spot... Does anyone know how...
2
by: aj70000 | last post by:
This is my query select ano,max(date),a_subject from MY_TAB where table_name='xyz' and ano=877 group by a_subject,ano order by a_subject ANO max(Date) A_Subject 877 2005-01-20...
14
by: signaturefactory | last post by:
I am trying the following query in and oleDbCommand: SELECT PartLocations.LocationName, Sum(PartsJournal.Quantity) AS SumOfQuantity, PartsJournal.PartsLotNumber FROM PartLocations INNER JOIN...
13
by: Lee | last post by:
Hello All, First of all I would like to say thank you for all of the help I have received here. I have been teaching myself Access for about 4 years now and I've always been able to find a...
1
by: stan | last post by:
The SQL below worked in Access 95 (V 7) is not working in the current version (V 2003). What is supposed to happen: 1) From my form I hit a button that calls an input box. 2) Underlying...
0
by: Chuck36963 | last post by:
Hi all, I've been working on a listing problem and I can't figure out how to work it out. I have looked far and wide on the web to find answers, but I'd like other peoples input on my project in...
22
by: Stan | last post by:
I am working with Access 2003 on a computer running XP. I am new at using Access. I have a Db with a date field stored as mm/dd/yyyy. I need a Query that will prompt for the month, ie. 6 for...
11
by: Stuart Owen | last post by:
Hi, I'm a relative beginner to Access, and have just found out how to construct a 'query by form' using the SQL, Select/From/Where approach. With the addition of a few wildcards, I've got a form with...
11
by: hedges98 | last post by:
The thread title probably isn't very clear but I can't think how to word my problem properly. Here goes: I have a form that has textboxes for users to enter information for first appointments and...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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.