Search code examples
sqlms-access

MS Access - Query - Required Forms for Each Employee


I have 3 tables, all SharePoint lists. I am trying to create a query that will show me all of the required DQ_File Forms that do not have an attachment in the DQ_File.

DQ_File_Lookup is a lookup table for the description field in the DQ_File. It also has the "DQRequired" flag I am looking for to see all of the required fields that do not have an attachment.

I have included a screen shot showing the table layouts and relations.

Any help would be appreciated, I am sure I am just overlooking something obvious.

A example would be as follows:

Employee Name | Document Name

You would have employee Joe and he has forms A,B,D out of a possible forms A,B,C,D,E,F so he would be missing forms C,E and F.

So the employee name would come from the employee table, and the document name needs to get passed through the DQ_File Table from the DQ_File_Lookup the way I thought to do it was to get it to show all documents from the DQ_File table that are missing, that I can do. But that only shows the information that has an entry. There are certain forms that are required for every employee that I want to be able to see if a employee is missing any of those forms.

Using what @June7 posted below I got it to work, and it now will show me all 15 documents that are required for every driver. But when I add the attachment field from DQ_File it shows them all as zero attachments, when I know some of them do indeed have attachments already.

Here is a screen cap showing this. Williams in particular should only have about 5 documents that should be on this list, but instead it is showing like all 15 are missing.

Here is the SQL from the combined query:

SELECT [qryEmployees+DQFileLookup].Last, [qryEmployees+DQFileLookup].Description, DQ_File.Attachment
FROM DQ_File RIGHT JOIN [qryEmployees+DQFileLookup] ON DQ_File.EmployeeNo = [qryEmployees+DQFileLookup].EmployeeCode
WHERE (((DQ_File.Attachment.FileURL) Is Null) AND (([qryEmployees+DQFileLookup].CURRENT)=True) AND (([qryEmployees+DQFileLookup].DRIVER)=True) AND (([qryEmployees+DQFileLookup].DQRequired)=True));

Solution

  • If you want to know which required docs employees do not have, then need a dataset of all possible combinations of employees/docs. Then match that dataset with DQ_File to see what is missing. The all combinations dataset can be generated with a Cartesian query (a query without JOIN clause) - every record of each table will associate with every record of other table.

    SELECT Employees.*, DQ_File_Lookup.* FROM Employees, DQ_File_Lookup;

    Then join that query with DQ_File.

    SELECT Query1.EmployeeID, Query1.First, Query1.Last, Query1.ID, Query1.Title, Query1.DQRequired, DQ_File.Description, DQ_File.EmployeeNo
    FROM DQ_File RIGHT JOIN Query1 ON (DQ_File.EmployeeNo = Query1.EmployeeID) AND (DQ_File.Description = Query1.ID)
    WHERE (((Query1.DQRequired)=True) AND ((DQ_File.EmployeeNo) Is Null));
    

    Advise not to use exact same field names in multiple tables. For instance, Title in DQ_File_Lookup could be DocTitle and Title in Employees could be JobTitle. And there will be less confusion if ID is not used as name in all tables.

    It seems unnecessary to repeat Title and [Compliance Asset ID] in all 3 tables.

    Strongly advise not to use spaces in naming convention. Title case is better than all upper case.