Hi All
Access 2002/2007
WinXP Pro
Or so I thought.... I've tried alternative combinations of Joins, Unions and group by's ..you name it... but I just can't get the results I need. But I just know this is an easy one...!
I have three tables: one for Organisations, one for Contacts and one for Addresses.
My requirements specify that Contacts can have 'Many' Addreseses and that 'Many' Contacts can have the same Address. Equally, Organisations can have 'Many' Addreseses and that 'Many' Organisations can have the same Address.
My Contacts, Organisations and Addresses all have their own 'Unique_No' (Autonumbered Key) and I use these to store relationships in a fourth table namely 'Addresses_Associations" which also has a Unique_No.
so:-
Addresses_Associations
--------------------------
Unique_No (AutoNum), Addresses_Unique_No,Contacts_Unique_No,Organisations_Unique_No
All good so far!
I populate a Combobox which displays, for any given Address, all the existing associations with Contacts (note: there are separate Comboboxes for Contacts and Organisations so dont worry about them for the moment). I have to join the Addresses_Associations table to the Contacts table as there are a number of status fields in the Association which denote use as a Home,Work,Construction Address etc.
OK, Peice of Cake! e.g. For Address No 15
SELECT TBl1.*, TBl2.Unique_No,Tbl2.Home,Tbl2.Work, Tbl2.Addresses_Unique_No,Tbl2.Record_Status FROM Contacts As Tbl1
INNER JOIN Addresses_Associations As Tbl2 ON TBL1.Unique_No = Tbl2.Contacts_Unique_No WHERE Tbl2.Addresses_Unique_No=15 ORDER BY TBL1.Name
In 'Edit' mode, I want to to extend the Combobox list to include ALL other contacts. The user can then pick one and click an 'add' button which runs off to SQL INSERT a record in the Associations table accordingly.
For guidance, I want to mark entries where they are already in use for any given address thus:-
SELECT (IIF(TBL2.Addresses_Unique_No = 15,'[Sel] ','')) + TBL1.Name As Name , TBl1.*, TBL2.Unique_No,Tbl2.Home,Tbl2.Work, Tbl2.Addresses_Unique_No, Tbl2.Record_Status FROM Contacts ...................
Ahh.. I thought, I'll simply change my INNER JOIN for a LEFT JOIN which I thought would return ALL Contacts but still show me where there was an entry in the Associations table for record 15 But it just returns the same results as an the INNER.
So I tried a UNION SELECT using the same SQL but removed the Tbl2.Addresses_Unique_No=15 from the UNION call... but this showed duplicate contacts where a Contact was associated with other Address records... I thought Union automatically removed duplicates absed on primary keys
...
I've tried with Group-Bys but I keep getting syntax errors complaining about aggregate fuctions?
Can anyone help please?
Access 2002/2007
WinXP Pro
Or so I thought.... I've tried alternative combinations of Joins, Unions and group by's ..you name it... but I just can't get the results I need. But I just know this is an easy one...!
I have three tables: one for Organisations, one for Contacts and one for Addresses.
My requirements specify that Contacts can have 'Many' Addreseses and that 'Many' Contacts can have the same Address. Equally, Organisations can have 'Many' Addreseses and that 'Many' Organisations can have the same Address.
My Contacts, Organisations and Addresses all have their own 'Unique_No' (Autonumbered Key) and I use these to store relationships in a fourth table namely 'Addresses_Associations" which also has a Unique_No.
so:-
Addresses_Associations
--------------------------
Unique_No (AutoNum), Addresses_Unique_No,Contacts_Unique_No,Organisations_Unique_No
All good so far!
I populate a Combobox which displays, for any given Address, all the existing associations with Contacts (note: there are separate Comboboxes for Contacts and Organisations so dont worry about them for the moment). I have to join the Addresses_Associations table to the Contacts table as there are a number of status fields in the Association which denote use as a Home,Work,Construction Address etc.
OK, Peice of Cake! e.g. For Address No 15
SELECT TBl1.*, TBl2.Unique_No,Tbl2.Home,Tbl2.Work, Tbl2.Addresses_Unique_No,Tbl2.Record_Status FROM Contacts As Tbl1
INNER JOIN Addresses_Associations As Tbl2 ON TBL1.Unique_No = Tbl2.Contacts_Unique_No WHERE Tbl2.Addresses_Unique_No=15 ORDER BY TBL1.Name
In 'Edit' mode, I want to to extend the Combobox list to include ALL other contacts. The user can then pick one and click an 'add' button which runs off to SQL INSERT a record in the Associations table accordingly.
For guidance, I want to mark entries where they are already in use for any given address thus:-
SELECT (IIF(TBL2.Addresses_Unique_No = 15,'[Sel] ','')) + TBL1.Name As Name , TBl1.*, TBL2.Unique_No,Tbl2.Home,Tbl2.Work, Tbl2.Addresses_Unique_No, Tbl2.Record_Status FROM Contacts ...................
Ahh.. I thought, I'll simply change my INNER JOIN for a LEFT JOIN which I thought would return ALL Contacts but still show me where there was an entry in the Associations table for record 15 But it just returns the same results as an the INNER.
So I tried a UNION SELECT using the same SQL but removed the Tbl2.Addresses_Unique_No=15 from the UNION call... but this showed duplicate contacts where a Contact was associated with other Address records... I thought Union automatically removed duplicates absed on primary keys
...
I've tried with Group-Bys but I keep getting syntax errors complaining about aggregate fuctions?
Can anyone help please?