Results 1 to 6 of 6
  1. #1
    sainttomn's Avatar
    sainttomn is offline Novice
    Windows 7 32bit Access 2000
    Join Date
    Jul 2011
    Location
    2 Gangster Planets and a Cowboy World
    Posts
    12

    First of the Month, Following 2 Months in Query

    Good afternoon,



    I am working on converting a database from OpenOffice into MS Access, manually (my employer finally converted). I have managed to convert all my tables and I am working on my queries next. This has been going well, thus far, except for one sticking point.

    the database records new employees and their start date (and dozens of other things).

    In OpenOffice, I was able to use a piece of SQL that would basically add a "probabation period" indicator field.

    For example:

    Employee start Date = 1/15/2011
    This employee will be in probation through January, and all of February and March. His indicator will stop as of 4/1/2011.

    Employee Start Date= 1/1/2011
    This employee will be in probation for January and February, and his indicator will stop on 3/1/2011.

    Basically, the indicator will need to be present until the first of the month, following 2 months of employment.

    I was able to accomplish this in OO using CASEWHEN and DAYOFMONTH commands, but I don't believe those work the same in Access.

    If anyone can offer any assistance, I would appreciate it. I'm decent with SQL, but I haven't used Access in quite a while and I'm re-learning as I go.

    Here's the basic code snipped in OpenOffice's SQL format, in case it might help:

    Select "MyTable".*, TO_CHAR(CAST(CASE WHEN MONTH("Employee_Start_Date") > 9 AND DAYOFMONTH("Employee_Start_Date") >= 2 THEN YEAR("Employee_Start_Date") + 1 WHEN MONTH("Employee_Start_Date") <= 10 THEN YEAR("Employee_Start_Date") ELSE YEAR("Employee_Start_Date") + 1 END || '-' || CASE WHEN MONTH("Employee_Start_Date") + 3 >= 13 AND DAYOFMONTH("Employee_Start_Date") >= 2 THEN RIGHT('0' || MONTH("Employee_Start_Date") + 3 - 12,2) WHEN MONTH("Employee_Start_Date") + 3 < 13 AND DAYOFMONTH("Employee_Start_Date") >= 2 THEN RIGHT('0' || MONTH("Employee_Start_Date") + 3,2) WHEN MONTH("Employee_Start_Date") + 2 >= 13 and DAYOFMONTH("Employee_Start_Date") = 1 THEN RIGHT('0' || MONTH("Employee_Start_Date") + 2 - 12,2) WHEN MONTH("Employee_Start_Date") + 2 < 13 AND DAYOFMONTH("Employee_Start_Date") = 1 THEN RIGHT('0' || MONTH("Employee_Start_Date") + 2,2) END || '-01' as DATE),'MM/DD/YYYY') as "Indicator" From "MyTable"

    Thank you,
    -Tomn
    Last edited by sainttomn; 07-06-2011 at 03:52 PM. Reason: Solved

  2. #2
    RedWolf3x is offline Advanced Beginner
    Windows 7 32bit Access 2003
    Join Date
    Jul 2011
    Posts
    31
    Have you looked at this Thread? Perhaps it will help.

    https://www.accessforums.net/access/...6th-14783.html

    -Red

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,602
    Google: access sql case when
    Here is one return
    http://bytes.com/topic/access/answer...-sql-ms-access
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    AFAIK, SQL doesn't have a case statement, but I am far from being a SQL wizard or even advanced user.

    My solution is to write a custom (VBA) function.

    Create a new standard module. Name it something like 'MyFunctions".
    Paste in the following code:

    Code:
    Function Get_EOP(pEmpStartDate As Date) As String
    'get end of probation date
    
       Dim eopYear As Integer
       Dim eopMonth As Integer
    
       If Month(pEmpStartDate) > 9 And Day(pEmpStartDate) >= 2 Then
          eopYear = Year(pEmpStartDate) + 1
       ElseIf Month(pEmpStartDate) < 10 Then
          eopYear = Year(pEmpStartDate)
       Else
          eopYear = Year(pEmpStartDate) + 1
       End If
    
       If Month(pEmpStartDate) + 3 >= 12 And Day(pEmpStartDate) >= 2 Then
          eopMonth = Month(pEmpStartDate) + 3 - 12
       ElseIf Month(pEmpStartDate) + 3 < 13 And Day(pEmpStartDate) >= 2 Then
          eopMonth = Month(pEmpStartDate) + 3
       ElseIf Month(pEmpStartDate) + 2 >= 12 And Day(pEmpStartDate) = 1 Then
          eopMonth = Month(pEmpStartDate) + 2 - 12
       ElseIf Month(pEmpStartDate) + 2 < 13 And Day(pEmpStartDate) = 1 Then
          eopMonth = Month(pEmpStartDate) + 2
       End If
    
       Get_EOP = Format(CStr(DateSerial(eopYear, eopMonth, 1)), "mm/dd/yyyy")
    End Function
    The SQL statemnet would be:

    Code:
    Select MyTable.*, GetEOP([Employee_Start_Date]) as Indicator From MyTable
    The VBA could be cleaned up... I tried to match what you have in the SQL to get it to return the same values.

  5. #5
    RedWolf3x is offline Advanced Beginner
    Windows 7 32bit Access 2003
    Join Date
    Jul 2011
    Posts
    31
    You could try using Switch.

    Switch(Month(Employee_Start_Date)>9 And Day(Employee_Start_Date)>=2,DateSerial(Year(Employ ee_Start_Date)+1,Month(Employee_Start_Date)-9,1),Month(Employee_Start_Date) And Day(Employee_Start_Date)=1,DateSerial(Year(Employe e_Start_Date),Month(Employee_Start_Date),2,1),Mont h(Employee_Start_Date)<=9 And Day(Employee_Start_Date)>=2,DateSerial(Year(Employ ee_Start_Date),Month(Employee_Start_Date)+3,1),Mon th(Employee_Start_Date)>10 And Day(Employee_Start_Date)=1,DateSerial(Year(Employe e_Start_Date)+1,Month(Employee_Start_Date)-10,1))

    This should work.

    -Red

  6. #6
    sainttomn's Avatar
    sainttomn is offline Novice
    Windows 7 32bit Access 2003
    Join Date
    Jul 2011
    Location
    2 Gangster Planets and a Cowboy World
    Posts
    12
    Thank you all for your help. Multiple excelent solutions!

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

Similar Threads

  1. Replies: 9
    Last Post: 04-27-2011, 05:04 PM
  2. Replies: 2
    Last Post: 08-25-2010, 01:42 PM
  3. Replies: 3
    Last Post: 04-07-2010, 11:25 AM
  4. Total all months in query or report
    By Brian62 in forum Queries
    Replies: 2
    Last Post: 10-23-2009, 08:41 AM
  5. Simple Report by Month
    By leejqs in forum Reports
    Replies: 3
    Last Post: 07-15-2009, 09:24 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