SQL Select query isn't working with my Access Form - sql

So I am doing a project where we are attempting to allow our user to open the form, select (via a combobox) an instructor, and then return all the students who have had that instructor over the years in a table.
Without the WHERE clause the code runs, returning all the students who have had that instructor. But when I use the [Forms]![STUDENT FORM]![INSTRUCTOR] in the WHERE clause we suddenly get no results. Can anyone explain why this is? It has worked in the same form with a different combobox, but we didn't need to use any joins, so I'm wondering if that's the issue?
This is the SQL code - our form is named STUDENT FORM and the combobox is named INSTRUCTOR:
SELECT STUDENT.STU_LNAME, STUDENT.STU_FNAME, STUDENT.[STU_ INT],
STUDENT.STU_NICK, STUDENT.STU_YEAR, STUDENT.STU_PHONE, STUDENT.STU_EMAIL,
INSTRUCTOR.INSTRUCTOR_LNAME FROM
(INSTRUCTOR INNER JOIN CLASS ON
INSTRUCTOR.[INSTRUCTOR_ID] = CLASS.[INSTRUCTOR_ID]) INNER JOIN
(STUDENT INNER JOIN ENROLL ON STUDENT.[STU_NUM] = ENROLL.[STU_NUM])
ON CLASS.[CLASS_ID] = ENROLL.[CLASS_ID]
WHERE (((INSTRUCTOR.INSTRUCTOR_LNAME)=[Forms]![STUDENT FORM]![INSTRUCTOR]));
Please let me know if you need any more information!

I bet your INSTRUCTOR combobox has two columns, with INSTRUCTOR_ID being the bound column and having width 0 (so only INSTRUCTOR_LNAME is visible).
So the value you need to filter for is INSTRUCTOR_ID, and you need to change the WHERE clause to:
WHERE (((INSTRUCTOR.INSTRUCTOR_ID)=[Forms]![STUDENT FORM]![INSTRUCTOR]));

Related

Access SQL Lookup - Dropdown Columns

I have looked for a similar issue with no luck. Maybe I don't know the right term to search for.
This seems so simple, but I just can't get it after spending many hours trying different approaches.
I have a dropdown to select contracts which shows some ids for related fields. How can I get those IDs to show the value of another column.
SELECT tbl_contracts.ID, tbl_contracts.contract_name, tbl_contracts.firm_id, tbl_contracts.agency_id
FROM tbl_contracts;
image of dropdown
I would like the IDs shown for agency_id and firm_id to list the company_name from their respective table "tbl_firm_agencies" where the tbl_contracts looks them up from. I've tried INNER JOINS but when I do, I can only line items to show when both agency AND firm exist, so my dropdown get cut off quite a bit.
Simply LEFT JOIN to those lookup tables as opposed to INNER JOIN. Adjust table and field names to actual ones in query below. Also, the parentheses are required in MS Access SQL.
SELECT c.ID, c.contract_name, f.firm_name, a.agency_name
FROM (tbl_contracts c
LEFT JOIN tbl_firms f
ON c.firm_id = f.firm_name)
LEFT JOIN tbl_agencies a
ON c.agency_id = a.agency_name;

MS Access SQL, sort by age in a specific order

My task: "Compile an SQL query that outputs a specific store (enter parameter window) the age of the youngest buyer"
I´ve tried some things, but because i´m new to SQL and i have no idea what i´m doing non of them seem to work.
I´d really appreciate, if someone would help me.
Thanks!
First you need to know the fields to SELECT (or return) and the table FROM which you are querying (asking) data; let's say you have the following tables: tblStores (containing a list of stores and related info), tblCustomers (containing customers and related info, e.g. ages, names, phone numbers, etc.), and tblPurchases (containing all the purchases at all stores by all customers and related info). You want the minimum age of a customer making a purchase at a specfic store, so you could use a MIN aggregating function. You would want to join (or relate) the tables based on customers and purchases. See my INNER JOINs in the example below. Then you filter the result by the user-inputted store name (inputStoreName) using WHERE; since the inputStoreName is undefined, in Access this would cause the parameter entry popup window to appear.
SELECT list of fields or aggregating functions you want (comma-separated)
FROM list of tables the fields are in (comma-separated) and how to join the tables
WHERE list of conditions to filter the data (separated by AND or OR)
Example:
SELECT tblStores.Name, tblStores.Description, MIN(tblCustomers.age)
FROM tblStores INNER JOIN ( tblPurchases INNER JOIN tblCustomers on tblPurchases.customerID = tblCustomer.customerID) ON tblStores.storeID = tblPurchases.storeID
WHERE (tblStores.Name = inputStoreName);
I recommend checking W3 schools. They are usually helpful for most programming tasks. If you provide more info about your database, we can provide more directed help.

Coding Inner Join subquery as field in query

After looking at example after example of both inner joins and subqueries as fields, I'm apparently not getting some aspect, and I would appreciate help please. I am trying to write one query that must, alas, run in MS Access 2007 to talk to an Oracle database. I have to get values from several different places for various bits of data. One of those bits of data is GROUP_CODE (e.g., faculty, staff, student, alum, etc.). Getting that is non-trivial. I am trying to use two inner joins to get the specific value. The value of borrower category must be the value for my main row in the outer query. Here is what this looks like:
Patron table Patron_Barcode table Patron_Group table
Patron_id Barcode Patron_Group_iD
Barcode Patron_Group_id PATRON_Group_Code
I want to get the PATRON_GROUP.PATRON_GROUP_CODE. This is only one of 35 fields I need to get in my query. (Yes, that's terrible, but wearing my librarian hat, i can't write the Java program I'd like to write to do this in a snap.)
So as a test, I wrote this query:
select PATRON.PATRON_ID As thePatron,
(SELECT PATRON_GROUP.PATRON_GROUP_CODE As borrowwerCategory
FROM (PATRON_GROUP
INNER JOIN PATRON_BARCODE ON PATRON_GROUP.PATRON_GROUP_ID = PATRON_BARCODE.PATRON_GROUP_ID
) INNER JOIN PATRON ON PATRON_BARCODE.PATRON_ID = thePatron.PATRON_ID
));
I don't know what I'm doing wrong, but this doesn't work. I've written a fair amount of SQL in my time, but never anything quite like this. What am I doing wrong?
PATRON.BARCODE is the foreign key for the BARCODE table.
PATRON_BARCODE.PATRON_GROUP_ID is the foreign key for the PATRON_GROUP table. PATRON_GROUP_CODE in PATRON_GROUP is he column value that I need.
PATRON.BARCODE -> BARCODE.PATRON_GROUP_ID -> PATRON_GROUP.PATRON_GROUP_CODR>
The main table, PATRON, will have lots of other things, like inner and outer join to PATRON_ADDRESS, etc., and I can't just do an inner join directly to what I want in my main query. This has to happen in a subquery as a field. Thanks.
Ken

How can I make a SQL (Query) that is set as a record source in a form to be called once?

Background: working with MS Access as a front end and SQL Server as a back end to organize data of every student found in a school.
Well I've been working on MS Access forms and I've set the record source as a query and set the intended text boxes , combo boxes , etc... to the property that was received.
Now the problem is that the queries for every student is being executed while on the form load or changes from one record to the other. I've come to this conclusion by viewing the SQL profiler and noticed that there is a large amount of queries being executed (It take approx. 14 seconds to load one record).
Is there a way to go around this?
Here is the SQL script that is being used to retrieve the the data (Set as a record source on the form itself).
SELECT
PERSON.id, PERSON.id_number,
Student.Student_Status, PERSON.name,
PERSON.surname, PERSON.dob, PERSON.address_1, PERSON.address_2,
PERSON.town, PERSON.mobile, PERSON.telephone, PERSON.postcode,
PERSON.id, PERSON.nationality, PERSON.dual_nationality,
PERSON.gender, Student.student_type, Student.mcast_email,
PERSON.euCitizen, PERSON.email, PERSON.NI_no,
PERSON.next_of_kin, Student.Form, course.course_name,
course.course_code, CourseYear.Year, Institute.Institute_name
FROM
((((PERSON
LEFT JOIN
Student ON PERSON.ID = Student.person)
LEFT JOIN
CourseYear ON Student.id = CourseYear.student)
LEFT JOIN
Yearlyprogramme ON CourseYear.course = Yearlyprogramme.id)
LEFT JOIN
course ON Yearlyprogramme.course = course.id)
LEFT JOIN
Institute ON course.Institute = Institute.id;
These properties that are retrieved are set to the text boxes and and combo boxes to show data. Is there a way to call this query once for each student without having to the a WHERE clause on the ID?
In my experience, Access queries with multiple LEFT JOINs on linked SQL Server tables often behave very badly.
If you can't avoid the LEFT JOINs, create a SQL Server view from your query, link it in Access, and use the linked view as record source of your form.

Is there a more efficient way to execute this nested SQL query?

I am writing a query to fill a select box on a user form and it works fine but when I look at the SQL, I feel like there should be a better, more efficient way to write it. Notice the two nested SELECTs below are from the same table.
I am wondering if there is an SQL trick I can add to my repertoire since I have to do queries like this from time to time. Or perhaps my approach is sloppy by some standards? Any feedback is appreciated.
Here is the SQL:
SELECT c.id, c.county_name, adr.county
FROM (SELECT id, county_name FROM counties WHERE state = (SELECT state FROM members WHERE id = 53)) AS c
LEFT JOIN (SELECT county FROM members WHERE id = 53) AS a ON c.id = a.county;
Here are the results (partial):
http://i.stack.imgur.com/Jawfa.png
The "3022" in the right column is member 53's county.
If needed, here is an explanation of the query and my intentions:
I have a members table where the county field is stored as an integer linking to a lookup table called counties. I also want to filter the county results to the member's state.
The query is filling an HTML select element and I want to pre-select the county of the member. The only information my PHP has available (without running a second query to get more) is the member id.
So the query needs to return the county id, the county name and some kind of identifier to let me know which record is matching for the member (I just went with the linking county field and my code will check for that).
The database is an MDB file accessed from a PDO ODBC connection.
You may need not need any subqueries but possibly a self-join:
SELECT counties.id, counties.county_name, members_1.county
FROM counties
INNER JOIN members ON counties.state = members.state
LEFT JOIN members_1 ON counties.id = members_1.county
WHERE members.id = 53 AND members_1.id = 53