Link to home
Start Free TrialLog in
Avatar of wgclark
wgclark

asked on

MS Access query returns multiple rows, cannot use Distinct as it must be updateable

Hello -

I've been struggling with this for a week and I'm stuck. Maybe I'm just approaching this all wrong. Any help would be appreciated.

I created a form which will allow users to search/filter the data by various fields and display the companies which meet the specified criteria. I've created a query to pull all my data, but it is returning multiple records when a company has multiple WorkTypes and/or ProjectTypes.

For example:

Company            WorkType      ProjectType
---------------------------------------------------------------
AAA Plumbing      Plumbing      Residential
AAA Plumbing      Plumbing      Commercial


Using DISTINCT returns a single record for each company, but then it's no longer updateable.

Note: Results must be updateable. However, the users will only be updating fields in tblCompany so I do not need to display anything from any of the other tables. I just need to search them.

Here is an abbreviated table structure.

tblCompany
------------------------
CompanyID_PK as Int
CompanyName as String
....tons of other fields


tblCompanyWorkTypes
------------------------
CompanyID_PK as Int
WorkTypeID_PK as Int


tblCompanyProjectTypes
------------------------
CompanyID_PK as Int
ProjectID_PK as Int


tblWorkTypes
------------------------
WorkTypeID_PK as Int
WorkDesc as String (e.g. Roofing, Plumbing, etc.)


tblProjectTypes
------------------------
ProjectTypeID_PK as Int
ProjectDesc as String (e.g. Residential, Commercial, etc.)

My query is as follows:

SELECT tblCompany.CompanyID_PK, tblCompany.Comments, tblWorkTypes.WorkDesc, tblProjectTypes.ProjectDesc
FROM tblProjectTypes RIGHT JOIN ((tblCompany LEFT JOIN (tblWorkTypes RIGHT JOIN tblCompanyWorkTypes ON tblWorkTypes.WorkTypeID_PK = tblCompanyWorkTypes.WorkTypeID_PK) ON tblCompany.CompanyID_PK = tblCompanyWorkTypes.CompanyID_PK) LEFT JOIN tblCompanyProjectTypes ON tblCompany.CompanyID_PK = tblCompanyProjectTypes.CompanyID_PK) ON tblProjectTypes.ProjectTypeID_PK = tblCompanyProjectTypes.ProjectTypeID_PK;




Again, any help would be very much appreciated.
Avatar of GRayL
GRayL
Flag of Canada image

I don't follow you.  If you only want to update info in tblCompany, why do it from a query involved with four other tables?  
ASKER CERTIFIED SOLUTION
Avatar of dqmq
dqmq
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry, forgot the trailing paren here:

1. In the AfterUpdate event of the combobox, apply a filter to the company form:
    Me.filter="CompanyID_PK in (Select CompanyID_PK
           From tblCompanyWorkTypes
           where workTypeID=" & THECOMBOBOX.value & ")"
Avatar of wgclark
wgclark

ASKER

In order to find the correct records in tblCompany the user will need to search on WorkDesc and ProjectDesc. Maybe I'm being overly complex about this. Here is the relationships.
 
 

relationships.jpg
Avatar of wgclark

ASKER

My situation is a little more complex than I led on, but this was exactly what I needed.
    Me.filter="CompanyID_PK in (Select CompanyID_PK
           From tblCompanyWorkTypes
           where workTypeID=" & THECOMBOBOX.value & ")""
 
Thanks dqmq!!!
>My situation is a little more complex than I led on

That's almost always the case... Happy Day