Hi guys….
I’m using Access 2003 to develop a Book lending system for a local Charity.
I’ve been using Access for about six months now, so I guess the “newbie” tag is still applicable!
Relevant to my question, I have the following Tables and Fields
Book Table – Book Id
Loan Table – Book Id, Borrower Id, Date Borrowed, Date Returned
I’m trying to develop SQL to enable me to determine the Books which a Borrower has already borrowed, and the books which are currently out on loan (to anyone). If I can then combine these lists into one query, I can generate a list of all Books which are currently on loan, or have previously been read by this Borrower. If I then run this as an “exclusion” query against the Book Table, I have a list of available Books.
I was hoping to generate Queries, then use the SQL view to get at and manipulate (slightly) the “specific” SQL to make it “general” SQL by using VBA to modify the Borrower Id.
In Access I have got the queries working for a specific Borrower id (71).
My problem is converting the working “Final” query to native SQL, because Access uses the name of the Union Query in the “Final” query.
I was hoping to replace the name of the Union Query with the actual SQL of the Union Query, but when I do that, I get a message “Syntax error in JOIN operation”.
On pressing OK, the first UNION operand is highlighted.
I’ve tried very hard to ensure I’ve got the “Union query name replacement” code correct (see below), but is what I’m trying to do illegal / immoral / just plain old silly?
The failing SQL is the final code below. Can anyone out there help me?
UNION query (“Combined subqueries for 71”)
(SELECT DISTINCT Loan.[Library Number] FROM Loan
WHERE Loan.[Date Returned] Is Null)
UNION
(SELECT DISTINCT Loan.[Library Number] FROM Loan
WHERE Loan.[Borrower Number]=71)
ORDER BY Loan.[Library Number];
UNION query as a single bracketed clause without the ORDER
((SELECT DISTINCT Loan.[Library Number] FROM Loan
WHERE Loan.[Date Returned] Is Null)
UNION
(SELECT DISTINCT Loan.[Library Number] FROM Loan
WHERE Loan.[Borrower Number]=71))
Access generated FINAL query with Union query exclusions
SELECT Book.[Book No], Book.[Library Number], Book.Title
FROM Book
LEFT JOIN [Combined subqueries for 71] ON Book.[Library Number] = [Combined subqueries for 71].[Library Number]
WHERE ((([Combined subqueries for 71].[Library Number]) Is Null));
Simplified Access generated BOOK query with union query exclusions
SELECT Book.[Book No], Book.[Library Number], Book.Title
FROM Book
LEFT JOIN [Combined subqueries for 71] ON Book.[Library Number] = [Combined subqueries for 71].[Library Number]
WHERE [Combined subqueries for 71].[Library Number] Is Null;
Simplified BOOK query with union query inserted
SELECT Book.[Book No], Book.[Library Number], Book.Title
FROM Book
LEFT JOIN ((SELECT DISTINCT Loan.[Library Number] FROM Loan
WHERE Loan.[Date Returned] Is Null)
UNION
(SELECT DISTINCT Loan.[Library Number] FROM Loan
WHERE Loan.[Borrower Number]=71))
ON Book.[Library Number] = ((SELECT DISTINCT Loan.[Library Number] FROM Loan
WHERE Loan.[Date Returned] Is Null)
UNION
(SELECT DISTINCT Loan.[Library Number] FROM Loan
WHERE Loan.[Borrower Number]=71)).[Library Number]
WHERE ((SELECT DISTINCT Loan.[Library Number] FROM Loan
WHERE Loan.[Date Returned] Is Null)
UNION
(SELECT DISTINCT Loan.[Library Number] FROM Loan
WHERE Loan.[Borrower Number]=71)).[Library Number] Is Null;
I’m using Access 2003 to develop a Book lending system for a local Charity.
I’ve been using Access for about six months now, so I guess the “newbie” tag is still applicable!
Relevant to my question, I have the following Tables and Fields
Book Table – Book Id
Loan Table – Book Id, Borrower Id, Date Borrowed, Date Returned
I’m trying to develop SQL to enable me to determine the Books which a Borrower has already borrowed, and the books which are currently out on loan (to anyone). If I can then combine these lists into one query, I can generate a list of all Books which are currently on loan, or have previously been read by this Borrower. If I then run this as an “exclusion” query against the Book Table, I have a list of available Books.
I was hoping to generate Queries, then use the SQL view to get at and manipulate (slightly) the “specific” SQL to make it “general” SQL by using VBA to modify the Borrower Id.
In Access I have got the queries working for a specific Borrower id (71).
My problem is converting the working “Final” query to native SQL, because Access uses the name of the Union Query in the “Final” query.
I was hoping to replace the name of the Union Query with the actual SQL of the Union Query, but when I do that, I get a message “Syntax error in JOIN operation”.
On pressing OK, the first UNION operand is highlighted.
I’ve tried very hard to ensure I’ve got the “Union query name replacement” code correct (see below), but is what I’m trying to do illegal / immoral / just plain old silly?
The failing SQL is the final code below. Can anyone out there help me?
UNION query (“Combined subqueries for 71”)
(SELECT DISTINCT Loan.[Library Number] FROM Loan
WHERE Loan.[Date Returned] Is Null)
UNION
(SELECT DISTINCT Loan.[Library Number] FROM Loan
WHERE Loan.[Borrower Number]=71)
ORDER BY Loan.[Library Number];
UNION query as a single bracketed clause without the ORDER
((SELECT DISTINCT Loan.[Library Number] FROM Loan
WHERE Loan.[Date Returned] Is Null)
UNION
(SELECT DISTINCT Loan.[Library Number] FROM Loan
WHERE Loan.[Borrower Number]=71))
Access generated FINAL query with Union query exclusions
SELECT Book.[Book No], Book.[Library Number], Book.Title
FROM Book
LEFT JOIN [Combined subqueries for 71] ON Book.[Library Number] = [Combined subqueries for 71].[Library Number]
WHERE ((([Combined subqueries for 71].[Library Number]) Is Null));
Simplified Access generated BOOK query with union query exclusions
SELECT Book.[Book No], Book.[Library Number], Book.Title
FROM Book
LEFT JOIN [Combined subqueries for 71] ON Book.[Library Number] = [Combined subqueries for 71].[Library Number]
WHERE [Combined subqueries for 71].[Library Number] Is Null;
Simplified BOOK query with union query inserted
SELECT Book.[Book No], Book.[Library Number], Book.Title
FROM Book
LEFT JOIN ((SELECT DISTINCT Loan.[Library Number] FROM Loan
WHERE Loan.[Date Returned] Is Null)
UNION
(SELECT DISTINCT Loan.[Library Number] FROM Loan
WHERE Loan.[Borrower Number]=71))
ON Book.[Library Number] = ((SELECT DISTINCT Loan.[Library Number] FROM Loan
WHERE Loan.[Date Returned] Is Null)
UNION
(SELECT DISTINCT Loan.[Library Number] FROM Loan
WHERE Loan.[Borrower Number]=71)).[Library Number]
WHERE ((SELECT DISTINCT Loan.[Library Number] FROM Loan
WHERE Loan.[Date Returned] Is Null)
UNION
(SELECT DISTINCT Loan.[Library Number] FROM Loan
WHERE Loan.[Borrower Number]=71)).[Library Number] Is Null;