Alright, I don't want to hear any laughing about this one
I realize that this query isn't likely the most efficient way of achieving my desired results, however it is the only way I can get it to work the way I need it to. Modifying my table structure is not an option, as A) I've written too much table dependent code to modify it now, and B) I don't think there's anything wrong with my table structure, and I'm happy with the way it works for me in general. Anyway, I'm going to post the general query that the search is modeled after, and then the code that builds that SQL to give an idea of how my search needs to work. It's fairly basic, but here's a rundown of my table layout anyway...
For this particular search I've got a main table that I'm searching (tbl_Initiative), which is linked via many to one relationships to 5 other tables. As far as I can tell, this part is fine--I've joined all of the above mentioned tables in my query (since even if I'm not searching on their values, I still need to return their values...) In addition to these many to one relationships, I have two complex one to many relationships to tbl_InitiativeBuildingBlock and tbl_InitiativeFundingSource. These two tables each contain a InitiativeID field as a primary key through which they are linked to tbl_Initiative, and then a BuildingBlockID or FundingSourceID field respectively which links (many-to-one) to either tbl_BuildingBlock or tbl_FundingSource. These two tables contain just a few records each which serve as data entry options (like in combo boxes and stuff) I have things setup this way because I have other tbl_********BuildingBlock/FundingSource tables which use the same tbl_BuildingBlock/FundingSource values. In the interest of Normalization, I have split tbl_BuildingBlock and tbl_FundingSource off into more generic multi-purpose tables, and just link the more specific tbl_InitiativeBuildingBlock and tbl_InitiativeFundingSource tables to them. I hope this is making sense so far...if not, the code will probably clear things up a bit (or make things more confusing!). Anyhow, I cannot join these two tables, since that would result in multiple duplicate Initiatives being returned when there are multiple tbl_InitiativeBuildingBlock and tbl_InitiativeFundingSource entries associated with a tbl_Initiative entry. I've worked around this by building two subqueries to search both tbl_initiativebuildingblock and tbl_initiativefundingsource, both of which join their respective supporting tables (tbl_BuildingBlock and tbl_FundingSource), and then I use the resulting recordset to search for Initiatives that are strictly associated with tbl_InitiativeBuildingBlock/InitiativeFundingSource records.
From an interface perspective I've dealt with the many to one relationships in the obvious fashion (textboxes/comboboxes), and have used two multiselect-listboxes to return the full contents of both tbl_BuildingBlock and tbl_FundingSource, which the user can select from, and then my contorted query uses the selections to restrict the returned initiatives...
Without further ado, here's the SQL Query that I'm basing all of this on:
The '1' and '2' values in there are just for testing purposes...
I'll post the code in a reply to this post...the forum is telling me my post is too long...(It's a sign
)
For this particular search I've got a main table that I'm searching (tbl_Initiative), which is linked via many to one relationships to 5 other tables. As far as I can tell, this part is fine--I've joined all of the above mentioned tables in my query (since even if I'm not searching on their values, I still need to return their values...) In addition to these many to one relationships, I have two complex one to many relationships to tbl_InitiativeBuildingBlock and tbl_InitiativeFundingSource. These two tables each contain a InitiativeID field as a primary key through which they are linked to tbl_Initiative, and then a BuildingBlockID or FundingSourceID field respectively which links (many-to-one) to either tbl_BuildingBlock or tbl_FundingSource. These two tables contain just a few records each which serve as data entry options (like in combo boxes and stuff) I have things setup this way because I have other tbl_********BuildingBlock/FundingSource tables which use the same tbl_BuildingBlock/FundingSource values. In the interest of Normalization, I have split tbl_BuildingBlock and tbl_FundingSource off into more generic multi-purpose tables, and just link the more specific tbl_InitiativeBuildingBlock and tbl_InitiativeFundingSource tables to them. I hope this is making sense so far...if not, the code will probably clear things up a bit (or make things more confusing!). Anyhow, I cannot join these two tables, since that would result in multiple duplicate Initiatives being returned when there are multiple tbl_InitiativeBuildingBlock and tbl_InitiativeFundingSource entries associated with a tbl_Initiative entry. I've worked around this by building two subqueries to search both tbl_initiativebuildingblock and tbl_initiativefundingsource, both of which join their respective supporting tables (tbl_BuildingBlock and tbl_FundingSource), and then I use the resulting recordset to search for Initiatives that are strictly associated with tbl_InitiativeBuildingBlock/InitiativeFundingSource records.
From an interface perspective I've dealt with the many to one relationships in the obvious fashion (textboxes/comboboxes), and have used two multiselect-listboxes to return the full contents of both tbl_BuildingBlock and tbl_FundingSource, which the user can select from, and then my contorted query uses the selections to restrict the returned initiatives...
Without further ado, here's the SQL Query that I'm basing all of this on:
Code:
SELECT tbl_Initiative.InitiativeID, tbl_Initiative.InitiativeName, tbl_Initiative.InitiativeStartDate, tbl_Initiative.InitiativeEndDate
FROM tbl_ActivityType RIGHT JOIN (tbl_Jurisdiction RIGHT JOIN (tbl_InitiativeType RIGHT JOIN (tbl_SponsorOrganization RIGHT JOIN tbl_Initiative ON tbl_SponsorOrganization.SponsorOrganizationID = tbl_Initiative.SponsorOrganizationID) ON tbl_InitiativeType.InitiativeTypeID = tbl_Initiative.InitiativeTypeID) ON tbl_Jurisdiction.JurisdictionID = tbl_Initiative.JurisdictionID) ON tbl_ActivityType.ActivityTypeID = tbl_Initiative.ActivityTypeID
WHERE tbl_Initiative.InitiativeID IN (SELECT tbl_InitiativeBuildingBlock.InitiativeID
FROM tbl_BuildingBlock INNER JOIN tbl_InitiativeBuildingBlock ON tbl_BuildingBlock.BuildingBlockID = tbl_InitiativeBuildingBlock.BuildingBlockID
WHERE tbl_BuildingBlock.BuildingBlockID = 1)
AND tbl_Initiative.InitiativeID in (SELECT tbl_InitiativeFundingSource.InitiativeID
FROM tbl_FundingSource INNER JOIN tbl_InitiativeFundingSource ON tbl_FundingSource.FundingSourceID = tbl_InitiativeFundingSource.FundingSourceID
WHERE tbl_FundingSource.FundingSourceID = 2)
ORDER BY tbl_Initiative.InitiativeID;
The '1' and '2' values in there are just for testing purposes...
I'll post the code in a reply to this post...the forum is telling me my post is too long...(It's a sign