Question
Any Access guru's out there that can help me out? Trying to figure out how to achieve the following: I want a query to go through each individual in the table of individuals and return those who have a true value in Christmas List field. This database is old and was created with the CompanyName as the primary key for table of companies (I know - bad design). To get around having a company with multiple locations they created an addressline field. That way if "Company y" opens a new location in San Diego, they add an entry of "Company Y - San Diego" and enter "Company Y" in the addressline. Then for individuals who don't have a company, they are entered in a dummy company entry of "Home". So ideally, what I want this query to do is to loop through, grab all the individuals who are supposed to be on the Christmas List, If the addressline field ISN'T NULL I want CompanyName to equal the AddressLine, but If CompanyName = "Home" I want CompanyName to equal "" (emptry string). Also some people have a overnight address, since their normal address is a P.O Box. The below query I have working for me mostly, however I am not clear on how to make the companyname blank in the returned results if CompanyName="Home".
SELECT tblIndividuals.FirstName, tblIndividuals.LastName, IIf([tblcompanies].[addressline] Is Not Null,[tblcompanies].[addressline],[tblcompanies].[companyname]) AS CompanyName, IIf([tblIndividuals].[MailingAddress1] Is Null,[tblCompanies].[MailingAddress1],[tblIndividuals].[MailingAddress1]) AS MailingAddress1, IIf([tblIndividuals].[MailingAddress1] Is Null,[tblCompanies].[MailingAddress2],[tblIndividuals].[MailingAddress2]) AS MailingAddress2, IIf([tblIndividuals].[MailingAddress1] Is Null,[tblCompanies].[MailingAddress3],[tblIndividuals].[MailingAddress3]) AS MailingAddress3, IIf([tblIndividuals].[MailingAddress1] Is Null,[tblCompanies].[MailingAddress4],[tblIndividuals].[MailingAddress4]) AS MailingAddress4, IIf([tblIndividuals].[MailingAddress1] Is Null,[tblCompanies].[City],[tblIndividuals].[City]) AS City, IIf([tblIndividuals].[MailingAddress1] Is Null,[tblCompanies].[State],[tblIndividuals].[State]) AS State, IIf([tblIndividuals].[MailingAddress1] Is Null,[tblCompanies].[Zip],[tblIndividuals].[Zip]) AS Zip
FROM tblIndividuals INNER JOIN tblCompanies ON tblIndividuals.CompanyName = tblCompanies.CompanyName
WHERE (((tblIndividuals.ChristmasList)=True))
Order by [tblIndividuals].[companyname];
I would love to have it sort by CompanyName Values (A-Z), THEN by Individual's last names (A-Z) if at all possible. I will not pretend to be great at SQL, so any help you could give me would be hugely appreciated. Thank you for your time.
Solution
Consider a UNION query, for example:
SELECT i.FirstName, i.LastName,
IIf(c.[addressline] Is Not Null,
c.[addressline],c.[companyname]) AS CompanyName,
c.[MailingAddress1] AS MailingAddress1,
c.[MailingAddress2] AS MailingAddress2,
c.[MailingAddress3] AS MailingAddress3,
c.[MailingAddress4] AS MailingAddress4,
c.[City] AS City,
c.[State] AS State,
c.[Zip] AS Zip
FROM tblIndividuals i
INNER JOIN tblCompanies c ON i.CompanyName = c.CompanyName
WHERE i.ChristmasList=True
AND i.[MailingAddress1] Is Null
UNION
SELECT i.FirstName, i.LastName,
IIf(c.[addressline] Is Not Null,c.[addressline],
c.[companyname]) AS CompanyName,
i.[MailingAddress1] AS MailingAddress1,
i.[MailingAddress2] AS MailingAddress2,
i.[MailingAddress3] AS MailingAddress3,
i.[MailingAddress4] AS MailingAddress4,
i.[City] AS City,
i.[State] AS State,
i.[Zip] AS Zip
FROM tblIndividuals i
INNER JOIN tblCompanies c ON i.CompanyName = c.CompanyName
WHERE i.ChristmasList=True
AND i.[MailingAddress1] Is Not Null
You can wrap the query in brackets to create a derived table that you can sort using the query design window.
SELECT FirstName, LastName, etc
FROM ( <union query> ) As q
Order by q.[companyname]