Results 1 to 10 of 10
  1. #1
    rkalapura is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Location
    New York
    Posts
    69

    Find Min and Max in a selected date range

    I have a HR database with a "Employee" table, where we keep all our employee records. The main fileds are EmplId, Lname, Fname, JoinDate, LastDay, JobStat and much more. JoinDate is the date when the employ join the company and LastDay is the date when employ resigns or fired or permanantly leaving company for any reason. Every year company has to submit some reports to the local Govt offices on the employee status data of full year, starting from 01 September to next year 01 Septemper. I set everything exactly as they need in a button click. But I am having difficulty to set one query where they need dates when we had the min and max number of employees during that year. The logic behind this is, we should have the count of employees on first of September of the selecting year, and whenever a new employee join the company, the count should increase by one and whenever an employee leaves the company, count should decrease by one. Then I need to find the dates when we had the min / max number of employees during that year. FYI, now 2000+ employee records are in the database. New people joining and also leaving day by day. I mean, from last Sept till date, 43 people join the company, out of which 24 of them already resigned. Appreciate your help to set up the query/Queries to solve this. Is this make sense or need to have more info on database?



    Thank you in advance. God Bless You.

  2. #2
    burrina's Avatar
    burrina is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383
    DCount("*","[YourTableName]","[DateField] = " & "Date() DateCriteria")
    = Nz(DMax("YourFieldName", "CustomerID")

    Hope this gets you started in the right direction.

    Good Luck With Your Project.

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Here's a sample database that should be helpful. I mocked up a table with slightly different field names than yours, but similar enough for test. I used a union query and a procedure with a temporary table. The database includes the mockup table and a module containing the procedure. The union query is set up in sql within the proc. The procedure is commented.

    Hope it's useful.
    Attached Files Attached Files

  4. #4
    rkalapura is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Location
    New York
    Posts
    69
    Thank you Burrina for assistance. But I am lost here. Can you please explain little bit more on this?

    My table name is "Employees". I have two date fields [joindate] and and [lastday]. What is the Date criteria I need to use? Dmax of which Field name and what is the customerid? All this make me confuse. Appreciate your help.

    Thank you and God Bless

  5. #5
    rkalapura is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Location
    New York
    Posts
    69
    Thank you Orange for your help. I have seen your sample database and it is working fine. I copied your VB code and edit it with my tables and date sources, it gives many errors. Frankly speaking, I am not very good in VB, but can understand. Is there any equvalant access 2010 macro functions available?
    Thank you

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    I don't use macros. It was built with AC2010 and saved as .mdb.

    If you are having questions, please ask. The vba isn't complex, so maybe just a few concepts.

    What errors are you getting?

  7. #7
    rkalapura is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Location
    New York
    Posts
    69
    Thank you Orange for your assistance. I have enclosed the modified code which I am using and getting the error. I may be missing something. Please have a look and advise. The error message snapshot also enclosed: FYI, my datas are coming from a form where the user select a year and the values store automatically in these unbound fields. Employee strength of the starting year is from text field [Text169] and its source is a querry. Dateto and Datefrom are the fields we get the starting year and ending year dates, depends the user select the year from selectyear combo.

    Sub GetMaxMinEmployees()
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim rs1 As DAO.Recordset 'for temp table
    Dim sCtr As Integer 'starting count at beginning of fiscal year let's say 15 employees
    Dim MaxMsg As String
    Dim MinMsg As String
    Dim strSQL_DROP As String
    Dim strSQL_Create As String
    10 Set db = CurrentDb
    'Delete existing copy of this table
    20 On Error Resume Next
    30 strSQL_DROP = "DROP TABLE tblEmpMaxMin;"
    40 DoCmd.RunSQL strSQL_DROP
    50 On Error GoTo GetMaxMinEmployees_Error
    60 strSQL_Create = "CREATE TABLE tblEmpMaxMin" & _
    " (ReviewDate DateTime, EmpCount int);"
    70 DoCmd.RunSQL strSQL_Create
    80 Set rs1 = db.OpenRecordset("tblEmpMaxMin")

    90 sCtr = [Forms]![summary vets-100]![Text169] ' employees on strength at start of fy picks from a text field of form ************
    ' **** The union query ***********
    Dim UnionQuery As String
    100 UnionQuery = "select fname,hiredate as Ddate, 1 as Cnt " _
    & "FROM employees where termdate is null and hireDate>=#[forms]![summary vets-100]![datefrom]# " _
    & "UNION SELECT employees.fname, employees.termdate AS ddate, -1 AS Cnt " _
    & "FROM employees " _
    & "WHERE (employees.termdate)<#[forms]![summary vets-100]![dateto]# and employees.termdate >=#[forms]![summary vets-100]![datefrom]# " _
    & "ORDER BY ddate;"
    110 Set rs = db.OpenRecordset(UnionQuery) '"Employees_Union")
    120 Do While Not rs.EOF
    'do stuff
    130 sCtr = sCtr + rs!Cnt
    'Debug.Print "Emp count on " & rs!dDate & " is " & sCtr 'for debugging
    140 rs1.AddNew
    150 rs1!ReviewDate = rs!dDate
    160 rs1!EmpCount = sCtr
    170 rs1.Update
    180 rs.MoveNext
    190 Loop
    200 rs1.Close 'rs1
    210 MaxMsg = "Max Employees " & DMax("EmpCount", "tblEmpMaxMin") & " on " _
    & DLookup("ReviewDate", "tblEmpMaxMin", "EmpCount =" & DMax("EmpCount", "tblEmpMaxMin"))
    220 MinMsg = "Min Employees " & DMin("EmpCount", "tblEmpMaxMin") & " on " _
    & DLookup("ReviewDate", "tblEmpMaxMin", "EmpCount =" & DMin("EmpCount", "tblEmpMaxMin"))
    230 Debug.Print MaxMsg & vbCrLf & MinMsg
    240 MsgBox MaxMsg & vbCrLf & MinMsg, vbOKOnly
    250 On Error GoTo 0
    260 Exit Sub
    GetMaxMinEmployees_Error:
    270 MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure GetMaxMinEmployees of Module AWF_Related " & vbCrLf & " in Line " & Erl
    End Sub

    It is not allowing me to attach a error snap shot. The error popup reads like this: "Error 3000 (Reserved error(-3201); there is no message for this error.) in the procedure GetMaxMinEmployees of Module AWF_Related in Line 110".

    Appreciate your help.

    Thank you and God Bless!

  8. #8
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Just saw this response.

    I used a number for sCtr.

    I built a union query in code and called the variable UnionQuery.

    The code was intended to give you a working example that you could modify as needed.

    Your form must be open for your code to get a value in sCtr.

  9. #9
    rkalapura is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Location
    New York
    Posts
    69
    It is connected to the afterupdate event of the combo in the form. The error line is 110. I dont know why?

  10. #10
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Please post a copy of your database -- remove anything private/confidential.

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

Similar Threads

  1. Replies: 1
    Last Post: 03-06-2013, 10:30 AM
  2. Replies: 3
    Last Post: 07-18-2012, 10:13 PM
  3. Date Range Report with Multiple Date Fields
    By StevenCV in forum Reports
    Replies: 13
    Last Post: 02-29-2012, 08:29 AM
  4. Dlookup to find value in a range
    By jobrien4 in forum Access
    Replies: 1
    Last Post: 09-26-2011, 11:00 AM
  5. find value of selected field in a table
    By Bic in forum Programming
    Replies: 1
    Last Post: 02-05-2010, 07:40 PM

Tags for this Thread

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