Results 1 to 7 of 7
  1. #1
    scott0_1 is offline Advanced Beginner
    Windows Vista Access 2013 32bit
    Join Date
    Apr 2018
    Posts
    82

    Query with same count returned to every record?

    Hi All,



    I have a fairly complicated query in Access I'm hoping to get some help for. Due to the nature of the DB, I will not be able to post any part of it here. I will try to put all relevant information in this post though.

    The English version of what I'm trying to do: We have several districts with several offices. Each employee takes courses. The user will select a district and a course. From there, I am going to create a pivot chart bar graph to display offices on the district on the x-axis and number of employees on the y-axis. The bar for each office will be separated into three: Completed, not completed and not active (in case employees are away).

    This will be drawn from the tables defined at the bottom of the post.

    The fields I'll be looking for in the query result are:

    tblDistrict.DistrictID, tblDistrict.DistrictName, tblOffice.OfficeID, tblOffice.OfficeName, tblCourseCatalogue.CourseID, tblCourseCatalogue.CourseName, tblEmployeesScheduled.Attended=True, Count of Employees at Office, Count of Inactive Employees at Office. Count of Employees at Office With the Selected Course, Count of Employees at Office Without Selected Course (=Total - With Course - Inactive).

    Getting some of this info is very basic, but doing the counts is throwing me off. I've gone as far as making a DAO recordset and a two dimensional array. I'm then copying the office name into the first column of the array and using DCount for the next four columns, all in a For/Next loop. I'm not at work yet, so I haven't tried that code, but I'm sure it's going to be buggy. And it just seems unnecessary. I should be able to do this in the SQL query... Right?

    Example query results in the table at the bottom of the post.

    Thanks for any help you can offer!

    Scott

    DistrictID DistrictName OfficeID OfficeName CourseID CourseName TotalEmp CountInactive CountWithCourse CountWithoutCourse
    1 North 1 NorthVanA 1 FirstAid 10 1 7 2
    1 North 2 NorthVanB 1 FirstAid 7 2 4 1


    tblDistrict tblOffice tblEmployee tblCourseCatalogue tblEmployeesScheduled
    DistrictID OfficeID EmpID CourseID CourseID
    DistrictName OfficeName EmpName CourseName EmpID
    Active (yes/no) Date
    OfficeID Attended
    Last edited by scott0_1; 05-09-2018 at 01:01 PM.

  2. #2
    Gicu's Avatar
    Gicu is offline VIP
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Your tblEmployee is missing OfficeID field, how can you know what office an employee works in? To get your data I would create separate totals (count) queries by linking tblEmployeesScheduled to tblEmployee and grouping by OfficeID and CourseID (equi join to get the ones that attended, left outer join to get the ones that didn't - having Is Null in the EmplID field from the tblEmployeesScheduled ), one totals query based on tblEmployee to get your inactive count and finally the last one that gets you the total number of employees by OfficeID. In this one to join all the other three (left outer join by OfficeID).

    Cheers,
    Vlad

  3. #3
    scott0_1 is offline Advanced Beginner
    Windows Vista Access 2013 32bit
    Join Date
    Apr 2018
    Posts
    82
    Quote Originally Posted by Gicu View Post
    Your tblEmployee is missing OfficeID field, how can you know what office an employee works in?
    You're right, I forgot to type it in. I typed all of that this morning around 05:30 after being awake for two hours... Out of memory! Surprisingly, that's all I forgot!!! The original post is updated.

    I just got out of a meeting about this DB, and my eyes can't keep straight, so I'm going for lunch, then I'll look over your suggestion.

    Thanks for the help!

  4. #4
    scott0_1 is offline Advanced Beginner
    Windows Vista Access 2013 32bit
    Join Date
    Apr 2018
    Posts
    82
    I can get the number of employees that have taken the course in one query, but if I try to get the count that hasn't, I get no records returned. I know it's the join I'm using, but I'm not sure how to write it. Right now I've got:THis is driving me crazy. I just threw a pen at my monitor. I think my co-worker next to me is scared.

    Here's my statement that joins and shows all. I know there's redundant stuff in it. I'm just trying to work through it:
    *tblEmployeeScheduled got changed from above to tblIndividualLearning

    Code:
    SELECT tblEmployee.EmpID, tblIndividualLearning.EmpID, tblIndividualLearning.CatelogID
    FROM tblEmployee LEFT JOIN tblIndividualLearning ON tblEmployee.[EmpID] = tblIndividualLearning.[EmpID]
    WHERE (((tblIndividualLearning.CatelogID)=19));
    If I add "Is Null" to tblIndividualLearning.EmpID, I get nothing:

    Code:
    SELECT tblEmployee.EmpID, tblIndividualLearning.EmpID, tblIndividualLearning.CatelogID
    FROM tblEmployee LEFT JOIN tblIndividualLearning ON tblEmployee.[EmpID] = tblIndividualLearning.[EmpID]
    WHERE (((tblIndividualLearning.EmpID) Is Null) AND ((tblIndividualLearning.CatelogID)=19));

    This gets me a grouped my office list of employees that have not had course #19. If I try to total by EmpID though, it just totals ALL EMPLOYEES, not just the ones in this list.

    Code:
    SELECT tblOffices.District, tblOffices.OfficeName, tblEmployee.EmpID
    FROM tblOffices INNER JOIN tblEmployee ON tblOffices.ID = tblEmployee.Office
    GROUP BY tblOffices.District, tblOffices.OfficeName, tblEmployee.EmpID
    HAVING (((tblOffices.District)=54) AND ((tblEmployee.EmpID) Not In (SELECT tblIndividualLearning.EmpID
    FROM tblEmployee LEFT JOIN tblIndividualLearning ON tblEmployee.[EmpID] = tblIndividualLearning.[EmpID]
    WHERE (((tblIndividualLearning.CatelogID)=19))
    )));

  5. #5
    Gicu's Avatar
    Gicu is offline VIP
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Hi Scott,

    Have a look at the attached file, look at the query qryCOUNTS and see if that is what you're after.

    Cheers,
    Vlad
    Attached Files Attached Files

  6. #6
    scott0_1 is offline Advanced Beginner
    Windows Vista Access 2013 32bit
    Join Date
    Apr 2018
    Posts
    82
    Vlad, thanks for the file, but unfortunately my company has our computers so tightly clamped down, I can't download it and open it. I'm lucky to be able to use Access and VBA!!!

    I did find a solution to my problem though. I used part of your first suggestion with multiple queries. I used one to total the number of employees, active, inactive at each location then I queried against that by course and just did some math inside the SQL statement (I didn't know that it was that easy to do, SQL is not my forte!).

    Thanks for the help!!!

  7. #7
    Gicu's Avatar
    Gicu is offline VIP
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Great to hear, basically I did the same (calculated the ones that did not take the course as the total employees minus the ones that did take it).

    Cheers,
    Vlad

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 04-06-2016, 05:37 AM
  2. Replies: 3
    Last Post: 11-16-2014, 01:53 AM
  3. VBA Query Record Count Error
    By dac214 in forum Programming
    Replies: 9
    Last Post: 03-07-2014, 03:36 PM
  4. count record in query
    By mikichi in forum Queries
    Replies: 1
    Last Post: 01-28-2014, 06:58 AM
  5. Replies: 1
    Last Post: 09-22-2010, 08:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums