query to show Active employees for a given time frame - ms-access

I am a little new to building this but have come a long way.
I have built a db using Access 2007. I have a table that shows the employees info:
Lname
Fname
Status
HireDate
TermDate
(Status: they are either inactive (potential Hires), Active or Terminated)
I can run a query that will show me all the employees by hire date or run one to show term dates.
We would like to have a query that will give us a count of how many drivers are still there within a given month.
Say Joe Smith was hired on 01/01/2008 and was terminated on 05/15/2011. If I ran a report in 2011 on May 31st how would I need to build the query to show this employee as being there in the month of May?
I have used >=Date() and others. I could use between #05/01/2011# and #06/01/2011# in the criteria under TermDate but if there is not a date there, nothing shows up. I have even dropped down a line and added "Null" and still nothing or I get all the employees that are still there and the ones that was terminated before the dates. I'm not sure what I am doing wrong.

I'm unsure about the logic for the filter criteria on this one. I think your goal is to identify all drivers who were on staff during any part of May 2011. My best guess is you need at least 2 conditions to identify them.
HireDate prior to June 1, 2011
TermDate either Null or >= May 1, 2011
If those conditions are sufficient, the SQL could be fairly easy.
SELECT e.Lname, e.Fname
FROM employees_info AS e
WHERE
e.HireDate < #2011-6-1#
AND
(
e.TermDate Is Null
OR
e.TermDate >= #2011-5-1#
);
It sounds like you're building the query in Design View ... which is a good and helpful feature. However, it's difficult to describe how to build that query in Design View. So I suggest you create a new query, switch to SQL View and paste in that SQL text. Replace employees_info with your actual table name, and fix any field names I misspelled.
If that query runs without error, you can flip back and forth between Design and SQL view, make a change in one, and examine how it is represented in the other view.
The SQL doesn't have to be formatted the way I wrote it. I chose that way in hopes it would make the WHERE logic clear. And if you make changes to the query from Design View, Access will reformat the SQL as it sees fit. However, the formatting change should not break the query.
I used yyyy-m-d format for the literal date values. That format avoids any possible confusion over which parts represent day and month, such as whether #05-01-2011# is intended to represent May 1st or Jan 5th. However, when you alter the query, Access may change them to mm-dd-yyyy format. (Sometimes its "helpful" impulses are annoying.)
I'm puzzled about one point. It seems you have one record per employee. If that is so, and an employee can leave for any reason and be re-hired later, it would be difficult to capture the different employment terms in a single record. If you're facing that situation, you may need to revise table designs.
If I misinterpreted your data, please show us a brief data sample, and the output you want from the query based on that sample. Good luck with this.

Related

Using Switch Function or nested iif in an Access Query

I have a small little Access program that sums hours of employees, with queries for Daily, Weekly, Monthly, and from the start of the year, all accessed from an easy to read form.
I have been asked to make an option group to filter employees by fulltime, seasonal, or both. But an issue I have is that I can only access the PAYRULENAME from the table which begins 'FT' for Fulltime such as FTADBRK1 but Seasonal can be a whole mishmash of possibilities, just not beginning with "FT". Still, I did not think this to be an issue.
Since I already have the original queries done, I thought I could simply add an additional column and constraint for each query instead of making additional queries. So I tried some various methods. Such as:
Switch([Forms]![MyForm]![EmpType].Value="FT",Left([PAYRULENAME],2)="FT",[Forms]![MyForm]![EmpType].Value="S",Left([PAYRULENAME],2)<>"FT"[Forms]![MyForm]![EmpType].Value="All",[PAYRULENAME])
Where EmpType is a textbox that programmatically gives a value for the option group instead of 1,2, or 3. This works and I don't think this is where the issue is.
But using the above switch function gives a -1 for Fulltime or a 0 for Seasonal (S) and doesn't filter the query. (The last option is fine - but it would be nice to give an FT or S in a column instead of the entire PAYRULENAME).
I have also tried nested iif statements to no avail. Here is one: IIF([Forms]![MyForm]![EmpType].Value="All",[PAYRULENAME],IIF([Forms]![MyForm]![EmpType].Value="FT",Left([PAYRULENAME],2)="FT",Left([PAYRULENAME],2)<>"FT"))
Any suggestions would be appreciated!

ConcatRelated Returning Multiple Values

I am new to MS Access and am trying to build a database that includes a text roll up (effectively summarising staff feedback collectively to the manager). I have set it up using ConcatRelated (http://allenbrowne.com/func-concat.html) and this works as expected. The only issue is that I cannot seem to ensure that only the first instance of a value is returned, rather than every instance. For example:
The function is bringing back the top view, but I only want it to bring back the Distinct values - please note, as this is a manager view, the same value can appear multiple times in the data retrieved. As such, I almost need the text to de-dupe if it is already there.
Full disclosure - my SQL is not great, so I am using the Expression Builder from the design view
Any help will be appreciated
EDIT for more detail:
The image shows a sample of the data and the output. In this, Mary is the team leader and so is responsible for the areas of John, Steven, Erin, and Harriet. The UID refers to the responsibility area.
As you can see, the dataset has the "minimum target" referenced against the area of responsibility and the output I am getting is duplicating the comments (which I presume is because the value is returned twice), but I am trying to ensure that commentary is summarised, not duplicated.
Note, in the summary output, I am not interested in the areas of responsibility, just the commentary against the staff names.
I hope that makes sense
Build a query that retrieves distinct Comments values for each person:
SELECT DISTINCT Person, Comments FROM table;
or
SELECT Person, Comments FROM table GROUP BY Person, Comments;
Now reference that query in ConcatRelated function as source for Comments values.
Be aware that calling a function like this in query along with aggregating can cause slow performance with large dataset.

Report builder 3.0 Using Reportitems!TexboxXX.Value sometimes creates multiple boxes. Why?

I have 6 Datasets each one is the same query but has a different WHERE clause based on employee type. They generate 6 tables. At the top of the report there is a summary table which uses reportitems!textboxXX.value to grab the totals for 2 particular fields from all the tables. I'm also using a StartDate and EndDate parameter. Each reportitems! expression in the table comes from a different dataset in the report if that is relevant.
When I run the report using dates from yesterday back to the 9th of May I get the desired output.
But when I go to the 8th I get multiple rows all the same.
and as I go even further back I get even more rows of the same information. To my knowledge nothing interesting or different happened on the 8th of May and the tables further down the report look exactly the same. Any idea what might be causing this? The design view looks like this if that helps. There's no grouping or filters on the table.
Still not certain about the mechanics behind this but I found a 'solution' so I figured I'd post it. I just made a new dataset for my summary tables where the query was simply SELECT 1. Now I get a single row every time.

Complex fill in the blanks query in MS Access 2010

I have looked and haven't found a method on here to do this. I am assuming my search is skewed and I just missed it, if this is the case, please let me know.
Anywhooo, I have a large and unwieldy report coming out of SAP every day. Because it will often have some strangeness, we import that into an Access database so we can keep an eye on the stuff we need in our department. I am using a combination of 6 fields to create a primary key in Access. The information in those fields is about the only thing consistent I get out of this SAP report, but the remainder of the data can be considered dynamic and can change from day to day. Usually this is a matter of filling in a few blanks, Occasionally this is changing existing data, and on rare occasions, it may involve deleting data out of a handful of fields.
The SAP report is around 130 columns of data, So I'm looking for an efficient way to roll in the changes without overwriting what folks put in there manually.
EDIT:
Here is the way this is used. SAP (for reasons I'm not going to go into) sometimes will have bad data show up in the daily report. We are using Access to track and put the correct data in to something that we can generate much more accurate summaries. What the users put in is to be considered true and accurate.
The transactions we are tracking can take a long time to complete. Most take around 30 days to complete. That's why I will have blank fields on one day, and several of them to be filled in on the next. We might not get any for the next few days and then a bunch more are filled in later. That is the normal flow.
What I have to account for is the odd occasion where a mistake is made early in the process. At a certain point, an error will break SAP's ability to update anything at all in the report we have to use.
I have 3 fields set up that trigger what my users daily work is going to be. There is a logical flow so that user 1 completes what he needs to do and then that record will show up on User 2's report. These fields will also stop the general update process in an exception report if there is a difference in what is coming in from SAP, and what is already in my database.
What I am looking for is some way to systematically fill in blank fields, on existing records in access. I do not want to overwrite if something is in a field, only the null values. I can do this on one field at a time, but each record has about 130 fields. I'm wondering if there is a way I could do this in just 1 query?
Thanks all! I hope the edit makes more sense now
A simple google for "Access SQL update null values" could have yeilded you what you need. But if all you need to do is fill constant values into empty fields then something like:
UPDATE Table SET Table.field1 = VALUE
WHERE Table.field2 is NULL;
Now if this data is different for each record based on; say data from another field, then you may need to write some VBA to build that value/string for you. But otherwise if you are JUST updating null fields to include data, then a simple UPDATE statement will do
EDIT Based on new info:
So if I'm understanding correctly: you have two tables. One table with the blank fields and another table that contains the values you need.
If this is the case, you can use a similar UPDATE statement, but use an inner join to get the data you need from table B to fill in table A
UPDATE TableA INNER JOIN TableB ON TableA.KeyField = TableB.KeyField
SET TableA.NullField = TableB.NotNullField
WHERE TableA.NullField Is NULL;

How do I write the Criteria in Query Design to equal last month's data?

I've looked on a couple different forums and I'm unable to find what I need they all have it listed in SQL View, and that is NOT what I want. I would like to do this in the Query Design as it is much easier for me.
I simply would like to make a query to display certain parameters of the database I maintain. And those parameters would equal last month's data.
I previously was able to successfully make a query displaying all information for the past year but can't figure out how to display just this past months.
The past year Criteria:
>DateAdd("yyyy",-1,Date())
Since that worked I tried doing this but it would not work for me:
>DateAdd("mmmm",-1,Date())
I'm sure it's something simple that I'm just not seeing here. Any help or recommendations are welcome.
Referencing the link provided by Fionnuala I have come up with
>DateAdd("m',-30,Date())
The problem being is that it queries all results for the past 30 days from Today's date. I wish to display only data from October ! While this can be done easily. I don't wish to manually go in this query every month and change certain parameters. I would like it to be automatic so or next month December I click on the query and it displays all 30 days of November's data. And there is no manual process of going back in and changing any of the criteria.
In the Query Designer you can use a Criteria: like this
>=DateSerial(Year(Date()),Month(Date())-1,1) And <DateSerial(Year(Date()),Month(Date()),1)
The corresponding SQL statement is
SELECT Donations.*
FROM Donations
WHERE (((Donations.DonationDate)>=DateSerial(Year(Date()),Month(Date())-1,1)
And (Donations.DonationDate)<DateSerial(Year(Date()),Month(Date()),1)));
If it was run on November 18, 2014 then it would effectively be
SELECT Donations.*
FROM Donations
WHERE (((Donations.DonationDate)>=DateSerial(2014,10,1)
And (Donations.DonationDate)<DateSerial(2014,11,1)));
Notes:
This query should be sargable and take advantage of an index on the date field (if one exists).
In case anyone is concerned about "month wrap-around", the DateSerial() function takes care of that for us. If the query was run in January 2015 then the first WHERE condition would indeed be ... >=DateSerial(2015,0,1) but that's okay because it returns 2014-12-01.