473,405 Members | 2,344 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,405 software developers and data experts.

How do I show records not in another table in a subform?

Hello,

I am currently about to give up after scouring the internet for how to do this, it's probably a relatively novice answer, but I am a relatively novice user.

I have three tables
Expand|Select|Wrap|Line Numbers
  1. tblEquipment: (P) equipmentID, equipmentDescription
  2. tblCompartment: compartmentCode, equipmentID, (P)compartmentAutoID
  3. tblCompartmentList: (P)compartmentCode, compartmentName
The equipment table is joined one to many to tblCompartment, and tblCompartmentList is joined one to many on tblCompartment.

I have a Form where users choose from a drop box a piece of equipment. Below that I have a sub form. In that subform I want to display each compartmentCode and compartmentName that has not been entered for that piece of equipment into tblCompartmentID.

Heres my code that doesn't seem to exist for that subform:
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.     tblCompartmentList.compartmentCode, 
  3.     tblCompartmentList.compartmentName, 
  4.     tblCompartment.equipmentID
  5. FROM 
  6.     tblCompartmentList 
  7. LEFT JOIN tblCompartment 
  8.     ON tblCompartmentList.compartmentCode = tblCompartment.compartmentCode
  9. WHERE 
  10.     (((tblCompartment.compartmentCode) Is Null));

What am I doing wrong? Is there a better way?
Nov 11 '09 #1
5 2240
Atli
5,058 Expert 4TB
Hey.

The simplest solution would probably be to use a Subquery in a NOT IN clause.
Something like:
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.     l.compartmentName,
  3.     l.compartmentCode
  4. FROM
  5.     tblCompartmentList AS l
  6. WHERE
  7.     l.code NOT IN(
  8.         SELECT
  9.             ic.compartmentCode
  10.         FROM
  11.             tblCompartment AS ic
  12.         WHERE
  13.             ic.equipmentID = $searchID
  14.     )
This is usually the best method to use when you need to find rows that don't match. JOINS are better for finding matching rows.
Nov 11 '09 #2
Thanks a lot, makes a lot more sense, the different between all the joins is confusing the hell out of me. There is another part of my database I thought was similar but I guess I was mistaken, the bad code is follows:
Expand|Select|Wrap|Line Numbers
  1. SELECT tblCurrentIssues.sampleNumber, tblSample.sampleNumber, tblIssues.IssueDescription
  2. FROM tblSample RIGHT JOIN (tblIssues INNER JOIN tblCurrentIssues ON tblIssues.issueID = tblCurrentIssues.IssueID) ON tblSample.sampleNumber = tblCurrentIssues.sampleNumber;

Basically There is a main form with a subform inside and the above is the recordsource of listbox inside the subform. It is supposed to populate the listbox with all of the issue ID's and descriptions of those issues that are linked to that sample.

The tables are tblSample -onetoMany- tblCurrentIssues - oneToMany - tblIssues


Nothing shows up, I tried changing the RIGHT join back to an INNER join but then all of the records in tblCurrentIssue get displayed for every single sample :/


Any ideas?
Nov 11 '09 #3
And one more thing, I'm still having a little trouble with the origional code you gave me, I have:

Expand|Select|Wrap|Line Numbers
  1. SELECT tblCompartmentList.compartmentName, tblCompartmentList.compartmentCode
  2. FROM tblCompartmentList
  3. WHERE (((tblCompartmentList.compartmentCode) Not In (SELECT tblCompartment.compartmentCode FROM tblEquipment INNER JOIN tblCompartment ON tblEquipment.equipmentID = tblCompartment.equipmentID;      )));

But it's not showing any records at all?
Nov 11 '09 #4
Atli
5,058 Expert 4TB
@CoreyReynolds
I see two problems in your code.

#1 In your sub-query, you don't specify an equipment to search for. Therefore, the entire table will be returned, including all the codes being used by all equipment.
This query is meant to search for codes missing for a single piece of equipment, so you must specify a single equipment ID in your sub-query.

#2 There is a semi-colon (;) trailing the sub-query. This is not allowed, and will cause an error. That query should be giving you an error, rather than an empty result set.
How are you executing the query? As a part of a web-application, perhaps?

Your query should look more like:
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.     l.compartmentName, 
  3.     l.compartmentCode
  4. FROM 
  5.     tblCompartmentList AS l
  6. WHERE 
  7.     l.compartmentCode Not In (
  8.         SELECT c.compartmentCode 
  9.         FROM tblEquipment AS eq
  10.         INNER JOIN tblCompartment AS c
  11.             -- Removed the ; from this line:
  12.             ON eq.equipmentID = c.equipmentID
  13.             -- Added this line to specify the equipment to be searched for:
  14.             AND eq.equipmentID = <insert ID>
  15.     );
Also, note the following changes:
  • The formatting. It's a lot easier to read the query in this form, wouldn't you agree? It makes it a lot easier to spot problems when code is properly formatted.
  • The AS clauses on the table names.
    If you intend to use a lot of JOINS or Sub-queries, giving long table names simple aliases will make the query easier to read, and make them shorter. (Every byte counts on high-traffic sites.)
  • You had a bunch of unnecessary parentheses around your WHERE clause, which I removed. No need to make things more complicated than is needed ;-)
Nov 12 '09 #5
Agreed agreed agreed,

It is working 100% now.

Sorry about not formatting everything properly (and not wrapping them properly for this forum, I didn't see your note to me about that until I already did it a second time like a moron).

Access puts the multiple parenthesis there though all on its own, I have no idea why.

I got to tell you Atli, I'm working up in the frozen dead north right now not anywhere near civilization and you've helped me more then you will ever know. Thanks again.
Nov 13 '09 #6

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

Similar topics

3
by: Ryan.Chowdhury | last post by:
This is a general question regarding the use of view and stored procedures. I'm fairly new to databases and SQL. I've created a SQL database using an Access Data Project ("ADP") and I'm...
2
by: Tony Williams | last post by:
I have two tables one lists the names of committees and the other is a list of documents they generate. I have a form based on the documents table which gives details of the document including...
3
by: Jason | last post by:
I am trying to filter records in a primary form based on records in related tables. The data in the related tables is being displayed in the primary form through subforms. To be more specific, I...
1
by: Mark | last post by:
This question refers to a main form with a continuous form subform. After an error occurs after entering several records in the subform, how can I delete all the data in the main form and all the...
2
by: John Baker | last post by:
Hi: I have a sub form which i would like to show all the records selected from a table. There may well be over 100 of them. For reasons I cant fathom, the form will only show six, and then...
2
by: Colm O'Hagan | last post by:
Hi there, I having a problem with a database I'm setting up, I would be delighted if someone out there could help. The database I'm setting up is a task register datebase, it will be used to...
0
by: Jason | last post by:
I have a primary form which is used to enter/edit data in a table named Test_Results. On this primary form there is a subform which displays site addresses. This subform is linked to the primary...
1
by: phaddock4 | last post by:
Being fairly inexperienced at Access 2000, i've been reading many posts here for the last several days, and testing myself to find the best approach to do the following in A2K: SET UP: I have...
4
by: sparks | last post by:
I am trying to fix a database that someone did about 4 yrs ago in access97. The main table just contains demographics and is on the main form of the database. It has a subform on a tab that...
3
by: wvmbark | last post by:
First time poster... I just found this forum and it appears there's plenty of people here that could make short work of problem that's been driving me absolutely bonkers for months. Every day we...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.